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!

  • 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 Aug 12 '19 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 Aug 25 '19 at 18:34
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;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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