0

I do apprecciate this may sound like the usual question, but it is not. Just to let you understand I do not need to install one of the many SEO Plugin and change it on the box before content is published.

My scenario is the following:

I have a page which Querystring may change the content shown on page and I'd like to do the same with meta description and title if that would the the case.

In a normal template file I would probably look for the querystring and stick an if condition. Problem is that template is not mine and when it will be updated my changes will be lost. So I thought to a slightly more complicate solution.

Using the Wordpress php_exec plugin I will embed the php lines in the content section delegating to them the task, but using normal Wordpress core functions. This is a must, because I can't use a simple if condition in the content area, not without having two meta descriptions or title in the page, with the latter added in the wrong place.

So what I need to do is understanding how Wordpress is adding both the meta in the final HTML. Is this something related to the add_action('wp_head', ... something here)?

As additional info, I am not using Yoast SEO plugin, so I can't rely on any of his additional function.

Thanks for your help Andrea

4
  • 1
    Where are you getting the info you want for the meta? From the post title, a meta box..?
    – Wyck
    Commented Jan 18, 2013 at 19:07
  • 2
    Andrea, you could have adapted the Question based on the feedback you received at StackOverflow. You make a couple of comments over there that would be helpful in this very Q. You can edit it whenever needed.
    – brasofilo
    Commented Jan 18, 2013 at 19:37
  • @brasofilo Good point. Let me do this. Commented Jan 19, 2013 at 15:59
  • @Wyck I will be hardcoding the info in any case, as the querystring will suggest me what I need to provide. But in any case that doesn't seem the most complicate part. Commented Jan 19, 2013 at 16:01

1 Answer 1

1

I don't understand your attempt to use PHP_Exec, especially by embedding it in the content section of the page. That is going to run much too late to alter anything in the <head> of the document. Here is the problem you are facing, starting from what looks to be your primary question:

So what I need to do is understanding how Wordpress is adding both the meta in the final HTML. Is this something related to the add_action('wp_head', ... something here)?

WordPress doesn't add the meta tags to the <head> of your document, at least not most of them. It does add a few things-- the admin bar stylesheet, meta generator, some feed stuff-- but not the title, not the description, not most of the meaty bits. Your theme does that, and may or may not do it in a way that you can manipulate easily. Ideally, you can use a few different hooks-- wp_head, the_title, couple of others maybe-- to add and subtract content to/from the <head> part of the document but some themes do not make it that easy. Sometimes things are hard coded into the theme.

Assuming everything is in your favor you can alter the title with:

function alter_title_wpse_82196($title) {
  global $_GET;
  // if condition for you parameter
  if (...) {
     $title = 'whatever';
  }
  return $title;
}
add_filter('the_title','alter_title_wpse_82196');

And add the description with something similar.

function add_descr_wpse_82196() {
  global $_GET;
  // if condition for you parameter
  if (...) {
     $description = 'properly constructed meta description tag';
  }
  echo $description;
}
add_action('wp_head','add_descr_wpse_82196');

But that is a rough guide. Theme peculiarities may cause trouble.

6
  • Hi there, thankd for your time in explaining this to me. Much appreciated. Just one little thing I don't understand. Any reason to use the add_filter for changing the title and the add_action for changing the description? What if the theme uses an SEO plugin that already include both the meta bits? Is the above code meant to replace them, or should I include some extra bit? Commented Jan 19, 2013 at 18:29
  • Filters return content for the calling code to use. Think of them as sort-of string builders, though technically you might be returning arrays or booleans or something else. Actions do stuff-- maybe redirect to a different template or echo content directly-- but don't return content. If two plugins try to do the same thing there could be conflict and I don't know that there is a general answer for resolving that conflict.
    – s_ha_dum
    Commented Jan 19, 2013 at 19:49
  • Not sure I completely understand what you mean. I mean, I'm quite confident there are plugins that make more than a add_filter call at the same time, or even add_action at the same time. Why could not I have two of the same type one after another? Commented Jan 19, 2013 at 22:32
  • I didn't say that you can't have multiple functions hooked to the same filter. WordPress core hooks multiple functions to the same filter. I said that when plugins try to do the same thing there might be conflicts and there may be no generic solution for resolving that conflict. A good plugin will try to minimize the chances of a conflict but there is only so much you can do. There are all kinds of ways to break things.
    – s_ha_dum
    Commented Jan 19, 2013 at 22:49
  • I see. Apparently I've been able to get into the idea, but still far away before getting a solution. I'm using the SEO Ultimate wordpress plugin and as far as I understood every plugin does a different implementation when it comes to the meta data. Although I can change the meta info, at the time I change it unfortunately it's too late and I can't get them at the right place. Event calling the wp_head() doesn't produce the expected results as all the head is there again. I can eventually move the code in the funtcions.php, but I don't know how to hook the seo ultimate call to make the change. Commented Jan 20, 2013 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.