2024-07-18 Fredrik, Fritz

WordPress-plugin

I dag är ingen vanlig dag, för i dag har jag gjort en egen liten WordPress-plugin :D.

Hurra, vad jag är bra! Nu kommer jag att göra fler pluginer.

<?php
/**
 * @package Names
 * @version 1.0.0
 */
/*
Plugin Name: Names
Description: Displays names on each day according to swedish calendar.
Author: Vanja Anderson
Version: 1.0.0
Author URI: https://vanjaswebb.se
*/

// Define plugin url
define( 'NAMES__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

function get_name() {
	// Get contents from json-file
	$name_src = file_get_contents( NAMES__PLUGIN_DIR . 'names.json');
	// Convert json to array
	$names = json_decode($name_src,true);
	// Get date, month and day
	$date = date("Y-m-d");
	$month = date("F");
	$day = date("j")-1;

	echo "<p id='names'><strong>".$date."&ensp;".$names[$month][$day]['special-day']."</strong>&ensp;<em>".$names[$month][$day]['name-day']."</em> har namnsdag</p>";
}

// Now we set that function up to execute in the head.
add_action( 'wp_head', 'get_name' );

// We need some CSS to position the paragraph.
function names_css() {
	echo "
	<style type='text/css'>
	#names {
		text-align: center;
		height: 20px;
		position: relative;
		top: 20px;
		margin-bottom: -24px;
		font-size: 12px;
		color: gray;
	}
	.block-editor-page #names {
		display: none;
	}
	@media screen and (max-width: 782px) {
		#names {
			display: none;
		}
	}
	</style>
	";
}

add_action( 'wp_head', 'names_css' );