0
votes

I have this code that creates a dynamic page title in a Wordpress template page:

$prodname = sanitize_text_field($_GET['prod_name']);

$prodname = esc_sql( $prodname );

global $wpdb;
$products = $wpdb->get_results("SELECT * FROM product_data WHERE prod_name='$prodname';");

add_filter('wp_title', 'assignPageTitle');

function assignPageTitle() {

    global $products;

    foreach($products as $product){

        echo "".$product->prod_page_title."";

    }

}

I would like to use a similar technique to do meta description and meta keywords too, but i'm not sure what the Wordpress hooks are for those two. Anyone able to help?

UPDATE:

This code works to add meta description and meta keywords just before the closing </head> tag

add_action( 'wp_head', 'assignMetaInfo' );

function assignMetaInfo() {

    global $products;

    foreach($products as $product){

        echo "<meta name='description' content='Test Description' />";

        echo "<meta name='keywords' content='Test Keywords' />";

    }

}
1
are you using any SEO plugin? - Raunak Gupta
I'm using Genesis framework running a custom child theme. Genesis has it's own SEO settings, but as these template pages are dynamic using data from a MySQL database, I can't really use it, likewise with other plugins like Yoast. Unless you know of a method to add dynamic content via a plugin? - sharpenedpixels

1 Answers

0
votes

This is the correct solution:

add_action( 'wp_head', 'assignMetaInfo' );

function assignMetaInfo() {

    global $products;

    foreach($products as $product){

        echo "<meta name='description' content='Test Description' />";

        echo "<meta name='keywords' content='Test Keywords' />";

    }

}