Correctly set meta description in header.php


Staff member
I'm building a website with wordpress and a bought template. I added some functionality to the options/page creation. One can set a general meta description in the options and a meta description for each page while creating it.

Although I'm entirely new to PHP, I managed to add everything necessary to my code. It wasn't all that hard and it works just fine. My questions are: Am I doing it right? How can I optimize my solution? What are the downsides of my approach?

<strong>HTML (header.php):</strong>

// Defining a global variable
global $page_meta_description;

// Initializing the variable with the set value from the page
$page_meta_description= get_post_meta($post-&gt;ID, MTHEME . '_page_meta_description', true);

// Add meta tag if the variable isn't empty
if ( $page_meta_description != "" ) { ?&gt;
    &lt;meta name="description" content="&lt;?php echo $page_meta_description; ?&gt;" /&gt;

&lt;?php }

// Otherwise add globally set meta description
else if ( of_get_option('main_meta_description') ) { ?&gt;
    &lt;meta name="description" content="&lt;?php echo of_get_option('main_meta_description'); ?&gt;" /&gt;
&lt;?php }

// Set global meta keywords
if ( of_get_option('main_meta_keywords') ) { ?&gt;
    &lt;meta name="keywords" content="&lt;?php echo of_get_option('main_meta_keywords'); ?&gt;" /&gt;
&lt;?php } ?&gt;