How to edit tags in head via plugin?
-
Hello,
I was wondering how can I change title, meta and other tags in <head> via plugin ? I have created interstitial page, that will show up when user clicks on an outbound link and before the exist page appears.Page is being created dynamically, so title and other info will not be same all the time.
add_action('wp_head', 'my_head'); // function my_head(){ echo '<title> $MyVar->title; </title> '; }
Above just adds additional title tag, and does not alter the current one.
Please advice.
-
Hello again,
It sounds like your interstitial page is themed, specifically the header.php template handles the head content? The solution would then be theme dependent. A generic solution would be to alter the header.php template to detect the interstitial condition and do one thing with the tags, otherwise do the usual thing.
There may be other solutions, depending. For instance, you can alter the title by hooking the filter ‘wp_title’. Depending on where the other tags are coming from, altering them may be just as easy, or rather difficult.
So how can I edit header.php file via plugin ? I would have no power to make manual changes to user’s theme manually.
Isn’t there a function or method where you just say “Hey, replace the value of title tag in <head> with following.” ?
Oh, a plugin. That’s different. The head tags are installation specific, varying by theme and installed plugins. The title tag in particular is easy, it’s always there and you can set it through the ‘wp_title’ filter. It’s the other tags that are an issue, I don’t think there’s any single reliable way to identify every tag and affect it’s content.
You may be able to affect other always there tags, but anything theme specific or inserted through wp_head() would be difficult to affect.
I have used this
add_action('wp_head', 'my_head'); // function my_head(){ echo '<title> $MyVar->title; </title> '; }
but that adds another <title>. does not replace the value of current <title> value.
You must hook ‘wp_title’, not ‘wp_head’. And you must return the output string, not echo it. As with everything in the head section, it is theme dependent. If the theme does not use wp_title() to display the title tag, the filter will not work.
Okay now I tried this:
add_action('wp_title', 'my_head'); function my_head(){ echo ' $MyVar->title; '; }
and it is still adding a new title tag, and not replacing the existed one…
…you must return the output string, not echo it.
ok that works. but here is another problem. It has globally overwritten the title.
I mean it had replaced title tag on all pages.. How can I fix that ? I only want it to change title to the page that has my plugin content.
This is progress at least. First of all, your action callback function needs to accept the default title passed to it as a parameter. Meaning insert a variable between the parenthesis () after the function name. Then return this variable when the function is finished.
You will need to place the plugin title assignment inside an if(){} conditional so it only alters the default title variable if the content is from your plugin. The difficult part may be how to determine if the condition of the plugin content is true. The default query sets a number of template tags such as is_home() and is_archive(), etc. to help you identify the correct condition. These may or may not help you. You may be able to detect the correct conditions from query variables or maybe even $_SERVER[‘REQUEST_URI’]. If all else fails, your plugin can set a global variable indicating the next content is from it. Then the title action callback can test for this condition and clear it if found.
ok I understand what you are saying, this would be my next step. I am actually moved forward from my last mentioned post but little behind what you suggested. (Sorry, this is first time I am working on WP plugin)
Here is how it looks like now:
add_action( 'wp_title', 'AR_seo_head' ); function AR_seo_head($seotitle){ echo $seotitle; }
Now this code exists at myplugin.php and I want to pass the value from [plugin_dir]/views/postfile.phtml
problem is myplugin.php is not communicating with that file.
I think I did not made myself clear enough, as I am confused myself too.
should I make SEOTITLE a global variable, or actual variable which contains the value of actual title ? Or what else I could do to make it work ?
Regard,
π Two steps forward, one step back. No worries.
It must be
return $seotitle;
not echo! Remember though that$seotitle
as used here will be ANY title to ANY WP page that uses the header template. This is OK, but possibly a point of confusion.The real issue as you have identified is getting your plugin to communicate with the ‘wp_title’ callback function. As much as we should avoid using globals, we likely have no alternative here. So go ahead and declare a global in your plugin. We can use the fact the global is set as a flag to to alter the page title. Since the ‘wp_title’ filter fires for any page request, to avoid cross talk, you must delay setting this global until just before your plugin’s page is output, and your ‘wp_title’ callback function must unset the global so that it is not mistakenly applied to another page.
Thus, part of the callback function portion of this scheme should look something like this:
global $AR_seo_title; if(isset($AR_seo_title)) { $seotitle = $AR_seo_title; unset($AR_seo_title); } return $seotitle;
There may be more to this eventually, or that may be all of it, except for the function declaration and filter hook of course.
Again, be sure your plugin assigns a value to
$AR_seo_title
as late as possible before the page is output.Even when i do global, my plugin.php file does not read it still..
What am I missing tho ?
its getting exhausted now.ok so I am doing this in the view:
global $AR_seo_title; $AR_seo_title = $SyndContent->title;
and this in parent plugin file:
AR_seo_head($AR_seo_title); add_action( 'wp_title', 'AR_seo_head' ); function AR_seo_head($x){ return $x; }
and when I do var_dump($AR_seo_title); on same page it returns NULL
You need to declare the global in every function you want it to be accessible. This seems to be different than any other programming language where it is declared once and is accessible anywhere. One of those PHP quirks.
If you insert the following above
return $x;
in your snippet above, you should see results:global $AR_seo_title; if(isset($AR_seo_title)) { $x = $AR_seo_title; unset($AR_seo_title); }
I didn’t get this to be honest.
There are two files and we they use MVC technology for this plugin. now my variable resides at View file and I believe add_action is at controller or maybe model. What I need to do is to pass value from view. And its just not working.
- The topic ‘How to edit tags in head via plugin?’ is closed to new replies.