WordPress.org

Ready to get started?Download WordPress

Forums

How to edit tags in head via plugin? (18 posts)

  1. hz-ar
    Member
    Posted 1 year ago #

    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.

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. hz-ar
    Member
    Posted 1 year ago #

    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." ?

  4. bcworkz
    Member
    Posted 1 year ago #

    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.

  5. hz-ar
    Member
    Posted 1 year ago #

    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.

  6. bcworkz
    Member
    Posted 1 year ago #

    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.

  7. hz-ar
    Member
    Posted 1 year ago #

    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...

  8. bcworkz
    Member
    Posted 1 year ago #

    ...you must return the output string, not echo it.

  9. hz-ar
    Member
    Posted 1 year ago #

    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.

  10. bcworkz
    Member
    Posted 1 year ago #

    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.

  11. hz-ar
    Member
    Posted 1 year ago #

    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,

  12. bcworkz
    Member
    Posted 1 year ago #

    :D 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.

  13. hz-ar
    Member
    Posted 1 year ago #

    Even when i do global, my plugin.php file does not read it still..
    What am I missing tho ?
    its getting exhausted now.

  14. hz-ar
    Member
    Posted 1 year ago #

    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

  15. bcworkz
    Member
    Posted 1 year ago #

    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);
    }
  16. hz-ar
    Member
    Posted 1 year ago #

    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.

  17. bcworkz
    Member
    Posted 1 year ago #

    MVC. There's a twist I was not expecting. I should of had a clue when you first mentioned 'view'. I'm afraid I cannot help with any MVC implications. All I can say is in a typical WP installation, a global assigned in a plugin is available to an action hook as long as both declare the global in their respective scopes. Thus the code as I recently illustrated will work in a normal WP installation.

    I cannot begin to suggest how to pass values from view back to model. Sorry :(

  18. hz-ar
    Member
    Posted 1 year ago #

    Hello again,
    I am trying to achieve same task via WordPress Custom page template. I tried same thing:
    add_action( 'wp_title', 'AR_seo_head' );
    function AR_seo_head($seotitle){ echo $seotitle; }

    but this time it does not work at all...

    Please guide.

Topic Closed

This topic has been closed to new replies.

About this Topic