0
votes

I have a very simple Plugin at hand that adds a Meta Description to the Blog. I have slightly modified it to display also the Description in OpenGraph Protocol and Dublin Core. That was the easy part.

But i found in the Search results that the Description is shown for each and every Page.

Now I would like to add a rule that could tell the Plugin if it is in a Posting, that it should just show the_excerpt.

My idea was to simply add some rules in the and be gone with it. But I had to find out that the Plugin is called with < ?php wp_head(); ? > As I always try things first I checked the Plugin and it seemed like I found a way to change that call from

add_action( 'wp_head', 'vsmd_meta_description' );

to

add_action( 'wp_head_desc', 'vsmd_meta_description' );

Now I add in the < head > a new call < ?php wp_head_desc(); ? > check if it works, define the rules

<?php
if ( is_single() ) {
  echo '<meta name="description" content="<?php the_excerpt(); ?>" />'."\n";
  echo '<meta name="DC.Description" content="<?php the_excerpt(); ?>" />'."\n";
  echo '<meta property="og:description" content="<?php the_excerpt(); ?>" />'."\n";
} elseif ( is_front_page() ) {
  echo '<?php wp_head_desc(); ?>';
} else {
  echo '';
}
?>

and be happy. Well, it is NOT that easy as it just shows in the Source only < ?php wp_head_desc(); ? > and so I am at a loss, as all I found was something about hooks what i completely don't understand at all. What i do realized is, that my change in the Plugins add_action, indeed removed it from being called by < ?php wp_head(); ? > .. So i believe that i am close to accomplishment, but right now i do not see how to get it done.

Might someone be able to enlighten me how i can call the Plugin?

Kindly Caylean

2

2 Answers

1
votes

Just change add_action( 'wp_head', 'vsmd_meta_description' ); to

    add_action( 'wp_head', 'vsmd_meta_description_new' );
        function vsmd_meta_description_new(){
          if ( is_single() ) {
      echo '<meta name="description" content="<?php the_excerpt(); ?>"   />'."\n";
      echo '<meta name="DC.Description" content="<?php the_excerpt(); ?>" />'."\n";
      echo '<meta property="og:description" content="<?php the_excerpt(); ?>" />'."\n";
    } elseif ( is_front_page() ) {
       // code here 
    } else {
      echo '';
    }
  }
0
votes

Thanks to Arshid KV who showed me how to edit the Plugin i was able to get what i was looking for. It is properbly not the perfect way, but working pretty well.

I used the WP Plugin Very Simple Meta Description as I wanted to have only the Description without all that SEO Hokus Pokus you get with many other Plugins.

The Plugin needed to be rewritten from

// include meta description in header 
function vsmd_meta_description() {
    $vsmd_meta = esc_attr( get_option( 'vsmd-setting' ) );
    $vsmd_descr = get_bloginfo( 'description' );
    if (empty($vsmd_meta)) {
        echo '<meta name="description" content="'.$vsmd_descr.'" />'."\n";
        echo '<meta name="DC.Description" content="'.$vsmd_descr.'" />'."\n";
        echo '<meta property="og:description" content="'.$vsmd_descr.'" />'."\n";
    }
    else {
        echo '<meta name="description" content="'.$vsmd_meta.'" />'."\n";
        echo '<meta name="DC.Description" content="'.$vsmd_meta.'" />'."\n";
        echo '<meta property="og:description" content="'.$vsmd_meta.'" />'."\n";
    }
}

to

// include meta description in header 
function vsmd_meta_description() {
    $vsmd_meta = esc_attr( get_option( 'vsmd-setting' ) );
    $vsmd_descr = get_bloginfo( 'description' );
    if ( is_single() ) {
    echo '';
    } elseif ( is_front_page() ) {
    echo '<meta name="description" content="'.$vsmd_meta.'" />'."\n";
    echo '<meta name="DC.Description" content="'.$vsmd_meta.'" />'."\n";
    echo '<meta property="og:description" content="'.$vsmd_meta.'" />'."\n";
    } else {
    echo '';
    }
}

As it showed me now only the Description on the Frontpage, but no more in Postings or other sites. After that i had to edit a bit in the Header to bring up the needed Description for Postings with a Rule and the_excerpt, what worked after a while but also needed a bit more tweaking to avoid any unnessasary Tags and Links that were created with the simple < ?php the_excerpt(); ? > So in the End i had the modified Plugin file and in the < head > Section just after < ?php wp_head(); ? > i added my rules:

<?php if ( (is_page()) || (is_single()) ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$post = $wp_query->post;
$descrip = strip_tags($post->post_content);
$descrip_more = '';
if (strlen($descrip) > 155) {
    $descrip = substr($descrip,0,155);
    $descrip_more = ' ...';
}
$descrip = str_replace('"', '', $descrip);
$descrip = str_replace("'", '', $descrip);
$descripwords = preg_split('/[\n\r\t ]+/', $descrip, -1, PREG_SPLIT_NO_EMPTY);
array_pop($descripwords);
$descrip = implode(' ', $descripwords) . $descrip_more;
echo '<meta name="description" content="'.$descrip.'"   />'."\n";
echo '<meta name="DC.Description" content="'.$descrip.'" />'."\n";
echo '<meta property="og:description" content="'.$descrip.'" />'."\n";
?>
<?php endwhile; endif; elseif(is_home()) : ?>
<?php endif; ?>

And now it Works exactly as i wanted it to.

Properbly it could get more pretty if I could get the Excerpt working in the Plugin too, as it would reduce the needed changes that would have to be done, but at least it is working right now and thats all i ever wanted.

Kindly Caylean