I would like to know if it is possible to dynamically change meta tags
from php. I have a site that has a wordpress “PAGE” that runs php code to dynamically create content. The php code can potentially create hundreds or thousands of pages dynamically – like a products page on an eCommerce site. The site is
I am somewhat new to wordpress and not sure if there is a php function to accomplish this. The <title> tag is already there and I want to
modify it for each page. The description and keywords meta tags are
not there and I need to add these. I am assuming there is a php function to add a meta tag.
I tried this line of code but it did not add any tags.
Not sure if this is what I need.
So to summarize I want to add or change the Title, Description and
Keywords meta tags on dynamically created wordpress pages.
I know there are meta tag plugins but my understanding is that these
are only for static wordpress pages.
If you can provide examples with code that would be a big help.
I am assuming there is a php function to add a meta tag.
Not within WordPress – no.
I tried this line of code but it did not add any tags
And did you create aaddMyMetaTagInfo() function to go with it?
I know there are meta tag plugins but my understanding is that these are only for static wordpress pages.
No – they work in all types of pages – static & dynamically generated.
Thanks for the reply.
I did not know I needed the addMyMetaTagInfo() function.
Not sure how to use it though. I don’t see any info on this function either.
I’m still unclear how a meta tag plugin would help me insert meta tag info dynamically unless it has some functionality that lets me provide the meta information as the user requests a new page. The information to be presented in the meta tag is not available until the user makes a selection that runs a MySQL query. Thus it can only be provided as the php code gets it.
Thank You for your assistance.
Sorry. Did not quite get what you were saying.
I was thinking there was another function I needed to run.
Yes, I did create my own addMyMetaTagInfo()
function. I should have added this to my question.
Here is the complete code I tried.
echo ‘<title>My Title</title>’;
echo ‘<meta name=”description” content=”My Description” />’;
echo ‘<meta name=”keywords” content=”My Keywords” />’;
I don’t see any info on this function either.
That’s because there is no addMyMetaTagInfo() function in WordPress core.
I resolved this by inserting my own php code in the header.php file
for the theme I was using. To clarify – the header.php file you want to alter is within the theme’s root folder. In this case the theme is called Finesse and I altered the header.php code as shown:
<title><?php $theme->meta_title(); ?></title>
/*Altered line – NOTE: I removed the <title> … </title> and inserted this line. You could comment out the <title> line if you want. build-meta-tags.php is my own code which uses information from the query string at the end of my URL. I used this to create the <title> as well as the description and keywords meta tags. Some of the information to use in the tags came from a MySQL query to a table which the user can alter for the various meta tag information they may want to show.*/
<?php include_once(‘build-meta-tags.php’) ?>
- The topic ‘Dynamically change meta tags via PHP’ is closed to new replies.