WordPress::在插件短代码中添加_操作

WordPress::在插件短代码中添加_操作,wordpress,Wordpress,我正在尝试做一个add_操作调用,但只在包含特定插件短代码的页面上进行 目前,我正在全方位的页面上得到它,而不仅仅是在我需要的页面上 class SomePlugin { public __construct() { add_action('wp_head', array(&$this, 'addMetatags')); } public function addMetatags() { echo '<meta property="og:type" c

我正在尝试做一个add_操作调用,但只在包含特定插件短代码的页面上进行

目前,我正在全方位的页面上得到它,而不仅仅是在我需要的页面上

class SomePlugin {
  public __construct() {
    add_action('wp_head', array(&$this, 'addMetatags'));
  }

  public function addMetatags() {
    echo '<meta property="og:type" content="article" ';
  }
}
class-SomePlugin{
公共构造(){
添加动作('wp_head',数组(&$this,'addMetatags');
}
公共函数addMetatags(){

echo'只需使用shortcode_exists函数即可

   <?php
add_action('init','prefix_check_short_code');

 function prefix_check_short_code() {
  if ( shortcode_exists( 'your_short_code' ) ) { 
         $SomePlugin = new SomePlugin();
    }
 } 
  ?> 

将此代码用于主题函数或主插件文件

wordpress网站上的功能URL为:

尝试下面的代码

class SomePlugin {
  public function __construct() {
    add_action('wp_head', array($this, 'addMetatags'));
  }

  public function addMetatags() {
    if ( is_page( 'about-me' ) ) { 
        echo '<meta property="og:type" content="article">';
    }
  }
}
$SomePlugin = new SomePlugin();
class-SomePlugin{
公共函数构造(){
add_action('wp_head',array($this,'addMetatags'));
}
公共函数addMetatags(){
如果(是页面('about me'){
回声';
}
}
}
$SomePlugin=新建SomePlugin();

经过一番挖掘,我找到了最好的(通用)解决方案:

class somePluginClass {    

    public function __construct()
    {
       add_action('wp_head', array(&$this, 'renderTags'));
    }

    public function renderTags() {
       global $post;
       if (has_shortcode($post->post_content, self::SHORT_CODE)) {
          echo '<meta property="[some-property]" content="[content]" />';
       }
    }

}
class somePluginClass{
公共函数构造()
{
添加动作('wp_head',数组(&$this,'renderTags');
}
公共函数renderTags(){
全球$员额;
if(有_短码($post->post_内容,self::SHORT_代码)){
回声';
}
}
}

它不起作用。插件总是注册的,我需要一些东西,当一个插件短码出现在帖子中时,它会采取行动。它仍然不正常,你的解决方案的主要问题是使用短码存在(“你的短码”)因为在它的源代码中检查所有注册的短代码,不仅在这个页面上,而且在所有活动的短代码上,这将导致它将始终执行。有可能得到更通用的东西吗?因为我知道短代码是什么。像上面这样的特定页面条件我使用了它,它可以工作,但只要说是否有可能检查当前页面的currentl我在当前帖子上激活了短代码,然后设置了元数据。我没有找到任何东西来获取这些信息。