最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ChangeSet Page Title and Meta Tags from Page Called within a Plugin

programmeradmin0浏览0评论

I'm using a set of query vars and rewrite rules to load a new page. My final call to the page looks like this:

add_filter( 'template_include', 'se343855_template_include', 50 );
function se343855_template_include( $template )
{
    $sign1 = get_query_var('sign1', false);
    $sign2 = get_query_var('sign2', false);

    if ( $sign1 !== false || $sign2 !== false ) {
        // if this code is located in plugin file
        $template =  plugin_dir_path(__FILE__) . '/pages/form.php';
    }
    return $template;
}

This works, and the form.php page looks similar to this:

<?php include_once(dirname( __FILE__ ).'/../shortcodes/functions.php'); ?>
<?php get_header(); ?>
    <div id="primary" class="content-area grid-parent mobile-grid-100 grid-75 tablet-grid-75">
        <main id="main" class="site-main">
            <div>Page</div>
        </main>
    </div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

I need to set the page title (i.e., the content between the <title></title>) and a few meta tags (description, author, etc.) from within the form.php page.

I've seen a few methods for changing the title, namely by filtering on 'pre_get_document_title', but I don't want the title to change on every page. I haven't found any consistent way to update/add meta tags.

Any help on this is greatly appreciated!

I'm using a set of query vars and rewrite rules to load a new page. My final call to the page looks like this:

add_filter( 'template_include', 'se343855_template_include', 50 );
function se343855_template_include( $template )
{
    $sign1 = get_query_var('sign1', false);
    $sign2 = get_query_var('sign2', false);

    if ( $sign1 !== false || $sign2 !== false ) {
        // if this code is located in plugin file
        $template =  plugin_dir_path(__FILE__) . '/pages/form.php';
    }
    return $template;
}

This works, and the form.php page looks similar to this:

<?php include_once(dirname( __FILE__ ).'/../shortcodes/functions.php'); ?>
<?php get_header(); ?>
    <div id="primary" class="content-area grid-parent mobile-grid-100 grid-75 tablet-grid-75">
        <main id="main" class="site-main">
            <div>Page</div>
        </main>
    </div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

I need to set the page title (i.e., the content between the <title></title>) and a few meta tags (description, author, etc.) from within the form.php page.

I've seen a few methods for changing the title, namely by filtering on 'pre_get_document_title', but I don't want the title to change on every page. I haven't found any consistent way to update/add meta tags.

Any help on this is greatly appreciated!

Share Improve this question asked Aug 3, 2019 at 14:08 DexterDexter 1076 bronze badges 2
  • In pre_get_document_title filter hook or wp_head action hook you can use conditional tags or check query vars (as in the above code) to change title / add meta tags only on one selected page. – nmr Commented Aug 12, 2019 at 11:37
  • @nmr - How do I do that with meta tags? I see how to modify the title, but there don't seem to be examples for a meta description. – Dexter Commented Aug 25, 2019 at 18:34
Add a comment  | 

1 Answer 1

Reset to default 1

Attach to wp_head action hoook function that will display meta tag. Inside that function you can:

  • add your own filter that will allow you to change the value,
  • or change tag value depending of your query vars (sign1, sign2, ...) or conditional tags.
add_action( 'wp_head', 'se344297_description_metatag_display', 0 );
function se344297_description_metatag_display()
{
    $site_descr = apply_filters( 'description_tag_filter', 'Sample description text.' );
    if ( !empty($site_descr) )
       echo '<meta name="description" content="' . esc_attr($site_descr) . '">';
}

add_filter( 'description_tag_filter', 'se344297_custom_description_metatag' );
function se344297_custom_description_metatag( $descr )
{
    // use conditional tags or check query var
    // here to override meta tag
    //
    $sign1 = get_query_var('sign1', false);

    if ( $sign1 == 'AAA' ) {
        $descr = 'AAA description text';
    }
    else if ( $sign1 == 'BBB' ) {
        $descr = 'Description in case of BBB';
    }

    return $descr;
}
发布评论

评论列表(0)

  1. 暂无评论