1

I have a plugin which generates a page automatically. Is there a way I can add functionality to this plugin which would allow me to set the meta description, keywords, and title for the page? I've looked into SEO plugins like Yoast and AIOSEO but I cannot seem to achieve the desired goal.

Here is the source (UPDATED):

<?php

function wpse_36395_metadesc() {
    return 'Your Description';
}

if($city){

    $args = array(
        'post_title'   => $city . " Electrician",
        'post_name'    => $city . "-electrician",
        'post_date'    => $_SESSION['cal_startdate'],
        'post_content' => $citycontent,
        'post_status'  => 'publish',
        'post_type'    => 'page',
        'post_parent'  => 11192
    );

    $city_id = wp_insert_post($args);

    add_filter( 'wpseo_metadesc', 'wpse_36395_metadesc' );
}
?>

Kind Regards,

Brandon

2
  • You could hook into wp_head, I suppose - but if you plan on the plugin being compatible with a wide variety of themes, this will likely result in duplicate meta tags... Apr 19, 2013 at 2:30
  • Johannes, I appreciate the info. I've seen some solutions on using wp_head with the <title> but what about using this method for meta description and keywords? Apr 19, 2013 at 2:36

2 Answers 2

1

Theoretically, you could hook into wp_head and echo your tags from within your callback:

function wpse_36395_meta_tags() {
    echo '<meta name="description" content="Your Description" />';
}
add_action( 'wp_head', 'wpse_36395_meta_tags' );

But note that if a theme already has the tag(s) hardcoded into its <head></head> section, you'll end up with duplicate tags.
Whether that's more desirable than missing yours on those pages, you will have to decide for yourself.
I don't think I'd keep a plugin installed that forces meta tags into my header.

Edit: As for your comment, I am not familiar with the WordPress SEO API at all, but those are simple filters, so take a look at add_filterand you should be able to use that API.

This should yield the desired result:

function wpse_36395_metadesc() {
    return 'Your Description';
}
add_filter( 'wpseo_metadesc', 'wpse_36395_metadesc' );

Note that now you make your plugin dependent on WordPress SEO, though it will gracefully decline, if that's not activated/installed.

3
  • Johannes, this looks like a promising solution to my question, however.. would you be able to tell me if there is another solution using Yoast's API documentation? He has function called wpseo_title and wpseo_metadesc that it would be nice to take advantage of. Otherwise I will try this and report back. yoast.com/wordpress-seo-api-docs Apr 19, 2013 at 2:58
  • Johannes, I updated the source code in my question. When the page is generated.. I look at the source of the page but unfortunately the description is not there. Even if I try using just wp_head I cannot produce any results. Do you see any errors with the plugin? Apr 19, 2013 at 3:44
  • Still looking for a solution :( Apr 19, 2013 at 19:09
0

Brandon,

This is how you'd do this with All in One SEO Pack:

<?php

if( $city ){

    $args = array(
    'post_title'   => $city . " Electrician",
    'post_name'    => $city . "-electrician",
    'post_date'    => $_SESSION['cal_startdate'],
    'post_content' => $citycontent,
    'post_status'  => 'publish',
    'post_type'    => 'page',
    'post_parent'  => 11192
    );

    $city_id = wp_insert_post($args);
    add_post_meta( $city_id, '_aioseop_title', $city . " Electrician" );
    add_post_meta( $city_id, '_aioseop_description', $citycontent );
    add_post_meta( $city_id, '_aioseop_keywords', 'city, electricians' );
}

I just picked sample values here, but those are the postmeta keys that All in One SEO Pack uses on posts / pages etc. Hope this helps!

Your Answer

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

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