【问题标题】:Use WordPress shortcode to add <meta> tags使用 WordPress 简码添加 <meta> 标签
【发布时间】:2012-03-22 10:14:44
【问题描述】:

我正在编写一个使用简码的简单 WordPress 插件。我希望包含短代码的页面具有特定的 &lt;meta&gt; 标签。这可能吗?如果是这样,有没有一种优雅的方式来做到这一点?

我知道我可以使用wp_head 钩子添加&lt;meta&gt; 标签,但我希望元标签内容与插件生成的字符串相匹配。我可以将所有代码移到标题中,但是我不确定以后如何从短代码中引用它。换句话说,当我使用过滤器在&lt;head&gt; 中声明一个变量时,它不适用于我使用简码调用的类方法。

有什么想法吗?

更新:

提出了一个很好的解决方案,其中短代码的处理函数将操作添加到 wp_head 挂钩:

add_shortcode('fakeshortcode', 'fakeshortcode_handler');
function fakeshortcode_handler() {

    function add_meta_tags() {
        //echo stuff here that will go in the head
    }
    add_action('wp_head', 'add_meta_tags');
}

这很好,但问题是 wp_head 发生在短代码被解析并添加操作之前(所以没有任何东西被添加到上面的代码中)。为了让它发挥作用,我借用了this post 中的解决方案。它基本上是一个“向前看”帖子的功能,看看是否有任何短代码出现。如果是,则 IT 添加add_action('wp_head'...

编辑: 我删除了关于如何传递变量的后续问题。 这是一个新问题here

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案1】:

    第一次尝试(不要使用这个...请参阅下面的“编辑”)

    首先,您需要使用以下内容设置短代码:

    add_shortcode( 'metashortcode', 'metashortcode_addshortcode' );
    

    然后,您将创建一个函数,您必须在其中添加一个钩子到 wp_head,类似这样:

    function metashortcode_addshortcode() {
        add_action( 'wp_head', 'metashortcode_setmeta' );
    }
    

    然后,您将在 wp_head 中定义您要执行的操作:

    function metashortcode_setmeta() {
        echo '<meta name="key" content="value">';
    }
    

    添加短代码[metashortcode] 应该会根据需要添加您的元数据。提供代码只是为了帮助您了解如何实现它。它没有经过全面测试。

    编辑:之前的代码只是一个概念,由于执行顺序的原因,它不能工作。这是一个可以得到预期结果的工作示例:

    // Function to hook to "the_posts" (just edit the two variables)
    function metashortcode_mycode( $posts ) {
      $shortcode = 'metashortcode';
      $callback_function = 'metashortcode_setmeta';
    
      return metashortcode_shortcode_to_wphead( $posts, $shortcode, $callback_function );
    }
    
    // To execute when shortcode is found
    function metashortcode_setmeta() {
        echo '<meta name="key" content="value">';
    }
    
    // look for shortcode in the content and apply expected behaviour (don't edit!)
    function metashortcode_shortcode_to_wphead( $posts, $shortcode, $callback_function ) {
      if ( empty( $posts ) )
        return $posts;
    
      $found = false;
      foreach ( $posts as $post ) {
        if ( stripos( $post->post_content, '[' . $shortcode ) !== false ) {
          add_shortcode( $shortcode, '__return_empty_string' );
          $found = true;
          break;
        }
      }
    
      if ( $found )
        add_action( 'wp_head', $callback_function );
    
      return $posts;
    }
    
    // Instead of creating a shortcode, hook to the_posts
    add_action( 'the_posts', 'metashortcode_mycode' );
    

    享受吧!

    【讨论】:

    • 这很酷。我没有意识到在 wp_head 操作发生之前已经解析了短代码。我马上试试这个。如果你是对的,我相信它会很好地解决我的问题!
    • 我的怀疑是正确的。 wp_head 操作在解析短代码之前触发。所以这个解决方案并不像描述的那样工作。但是,可以修改该方法... [请参阅上面的更新]
    • 对不起,艾默生,我误解了你想要做什么。它之前没有执行。因此,我编辑了我的解决方案以使其正常工作。
    • 我想我们正在同时编辑同一篇文章......不幸的是,我想我已经覆盖了你的更改。对不起!
    • 感谢您的好主意!一条评论:你不应该使用'__return_true',而应该使用'__return_empty_string'。使用“__return_true”时,帖子内容中会出现“1”:D。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    相关资源
    最近更新 更多