$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Get post content inside plugin class method|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Get post content inside plugin class method

matteradmin11PV0评论

I'm writing a simple SEO class that will add the various metatag to the header of wordpress. I've two little problems with the wordpress api. The first is that I need to remove the last comma from the tag list, I've tried with rtrim but without success. The secont problem is that I'm unable to load the content of the post or page to create the content of the description meta tag. How I can fix these two issues?

<?php

/*
* Plugin Name: Custom Meta Tag
*/

class WP_Custom_Meta_Tag{

  private $post_tags;
  private $key;
  private $keyword;
  private $description;

  public function init(){
    add_action('wp_head', array($this, 'add_meta_description'));
    add_action('wp_head', array($this, 'add_meta_keywords'));
  }

  /*
  * metatag keywords
  */
  public function add_meta_keywords(){
    if( is_single() ){
      $post_tags = get_the_tags( $post->ID );
      $key = '';
      foreach( $post_tags as $keyword ){
        $key .= $keyword->name.', ';
      }
      echo '<meta name="keywords" content="'.$key.'">';
    }

  }

  /*
  * metatag description
  */
  public function add_meta_description(){
    if( is_single() ){

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\n", "\r", "\t"), ' ', $description );
      #$description = substr( $description, 0, 125 );

      #echo '<meta name="description" content="'. $description .'">';
      var_dump($d);
    }
  }

}

$meta = new WP_Custom_Meta_Tag;
$meta->init();
?> 

EDIT

I've fixed the issue, and now all seems to work correctly. If anyone has a suggestion to better trim the description text, please share an answer. I've read about wp_trim_words, but it will not work. Here is the updated code:

class WP_Custom_Meta_Tag{

  private $post_tags;
  private $key;
  private $keyword;
  private $description;

  public function init(){
    add_action('wp_head', array($this, 'add_metatags'), 1);
  }

  /*
  * metatag keywords
  */
  public function add_metatags(){
    global $post;
    if( is_single() || is_page() ){
      $post_tags = get_the_tags( $post->ID );
      $key = [];
      foreach( $post_tags as $keyword ){
        $key[] = $keyword->name;
      }
      // this part needs to be improved
      $description = strip_tags( $post->post_content );
      $description = strip_shortcodes( $post->post_content );
      $description = str_replace( array("\n", "\r", "\t"), ' ', $description );
      $description = substr( $description, 0, 140 );

      echo '<meta name="description" content="'. $description .'" />';
      echo '<meta name="keywords" content="'.implode(',' ,$key).'" />';
      echo '<meta property="og:title" content="'. get_the_title( $post->ID ) .'" />';
      echo '<meta property="og:description" content="'. $description .'"/>';
      #echo '<meta property="og:type" content="" />';
      echo '<meta property="og:url" content="'. get_the_permalink( $post->ID ) .'" />';
      if( has_post_thumbnail() ){
        echo '<meta property="og:image" content="'. get_the_post_thumbnail_url( $post->ID , 'full') .'" />';
      }
    }
  }

}

$meta = new WP_Custom_Meta_Tag;
$meta->init();
?>

I'm writing a simple SEO class that will add the various metatag to the header of wordpress. I've two little problems with the wordpress api. The first is that I need to remove the last comma from the tag list, I've tried with rtrim but without success. The secont problem is that I'm unable to load the content of the post or page to create the content of the description meta tag. How I can fix these two issues?

<?php

/*
* Plugin Name: Custom Meta Tag
*/

class WP_Custom_Meta_Tag{

  private $post_tags;
  private $key;
  private $keyword;
  private $description;

  public function init(){
    add_action('wp_head', array($this, 'add_meta_description'));
    add_action('wp_head', array($this, 'add_meta_keywords'));
  }

  /*
  * metatag keywords
  */
  public function add_meta_keywords(){
    if( is_single() ){
      $post_tags = get_the_tags( $post->ID );
      $key = '';
      foreach( $post_tags as $keyword ){
        $key .= $keyword->name.', ';
      }
      echo '<meta name="keywords" content="'.$key.'">';
    }

  }

  /*
  * metatag description
  */
  public function add_meta_description(){
    if( is_single() ){

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\n", "\r", "\t"), ' ', $description );
      #$description = substr( $description, 0, 125 );

      #echo '<meta name="description" content="'. $description .'">';
      var_dump($d);
    }
  }

}

$meta = new WP_Custom_Meta_Tag;
$meta->init();
?> 

EDIT

I've fixed the issue, and now all seems to work correctly. If anyone has a suggestion to better trim the description text, please share an answer. I've read about wp_trim_words, but it will not work. Here is the updated code:

class WP_Custom_Meta_Tag{

  private $post_tags;
  private $key;
  private $keyword;
  private $description;

  public function init(){
    add_action('wp_head', array($this, 'add_metatags'), 1);
  }

  /*
  * metatag keywords
  */
  public function add_metatags(){
    global $post;
    if( is_single() || is_page() ){
      $post_tags = get_the_tags( $post->ID );
      $key = [];
      foreach( $post_tags as $keyword ){
        $key[] = $keyword->name;
      }
      // this part needs to be improved
      $description = strip_tags( $post->post_content );
      $description = strip_shortcodes( $post->post_content );
      $description = str_replace( array("\n", "\r", "\t"), ' ', $description );
      $description = substr( $description, 0, 140 );

      echo '<meta name="description" content="'. $description .'" />';
      echo '<meta name="keywords" content="'.implode(',' ,$key).'" />';
      echo '<meta property="og:title" content="'. get_the_title( $post->ID ) .'" />';
      echo '<meta property="og:description" content="'. $description .'"/>';
      #echo '<meta property="og:type" content="" />';
      echo '<meta property="og:url" content="'. get_the_permalink( $post->ID ) .'" />';
      if( has_post_thumbnail() ){
        echo '<meta property="og:image" content="'. get_the_post_thumbnail_url( $post->ID , 'full') .'" />';
      }
    }
  }

}

$meta = new WP_Custom_Meta_Tag;
$meta->init();
?>
Share Improve this question edited Mar 9, 2019 at 16:41 ZWPDev asked Mar 9, 2019 at 12:57 ZWPDevZWPDev 1162 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I need to remove the last comma from the tag list

Put all $keyword->name into an array and use implode() to get the result.

I'm unable to load the content of the post or page

Object $post is not available in your function. Use global $post; to access $post->ID and $post->post_content etc.

So,

public function add_meta_keywords(){
    if( is_single() ){
      $post_tags = get_the_tags( $post->ID );
      $key = '';
      foreach( $post_tags as $keyword ){
        $key .= $keyword->name.', ';
      }
      echo '<meta name="keywords" content="'.$key.'">';
    }

  }

should be something like this

public function add_meta_keywords(){
    if( is_single() ){

      global $post;

      $post_tags = get_the_tags( $post->ID );
      $key = array();
      foreach( $post_tags as $keyword ){
        $key []= $keyword->name;
      }
      echo '<meta name="keywords" content="'.implode(', ',$key).'">';
    }

  }

And

public function add_meta_description(){
    if( is_single() ){

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\n", "\r", "\t"), ' ', $description );
      #$description = substr( $description, 0, 125 );

      #echo '<meta name="description" content="'. $description .'">';
      var_dump($d);
    }
  }

should be

public function add_meta_description(){

    global $post;

    if( is_single() ){
      global $post;

      #$description = strip_tags( $post->post_content );
      $d = get_the_content( $post->ID );
      #$description = strip_shortcodes( $post->post_content );
      #$description = str_replace( array("\n", "\r", "\t"), ' ', $description );
      #$description = substr( $description, 0, 125 );

      #echo '<meta name="description" content="'. $description .'">';
      var_dump($d);
    }
  }

EDIT:

Better solution to trim the description to 140 characters.

TRY this

// Checks the content's length, if more 140 chars...
// Output  140 chars, but don't break a word

if ( strlen($description) > 140){

    $description = substr($description, 0, strpos($description, ' ', 140));
}

echo '<meta name="description" content="'. $description .'">';

I hope this helps.

Post a comment

comment list (0)

  1. No comments so far