简体   繁体   English

将wordpress插件挂钩到单个页面以呈现一些html / javascript内容的最佳方法是什么?

[英]What is the best way to hook wordpress plugin to a single page in order to render some html/javascript content?

I have developed a WordPress plugin which renders some dynamic html and javascript content. 我开发了一个WordPress插件,它可以呈现一些动态的html和javascript内容。 I would like to know how I should modify it so that it can be hooked to a single page in my wordpress application. 我想知道我应该如何修改它,以便它可以挂钩到我的wordpress应用程序中的单个页面。

An idea I had was to make a bind the plugin function to a custom hook, and call this hook on the page, however, I don't know how to call the hook from a specific page. 我的想法是将插件函数绑定到自定义挂钩,并在页面上调用此挂钩,但是,我不知道如何从特定页面调用挂钩。 Or hook it to the part of the page you want and then use a conditional statement to only render if i is the correct page? 或者将它挂钩到你想要的页面部分,然后使用条件语句只渲染我是否是正确的页面? This seems annoying because I want to be able to use my plugin on any page without modifying it. 这看起来很烦人,因为我希望能够在任何页面上使用我的插件而无需修改它。 Is there a way I can pass the desired page to it as an argument? 有没有办法可以将所需的页面作为参数传递给它?

I am looking for the "best practice" solution so I apologize if the question is a bit broad. 我正在寻找“最佳实践”解决方案,所以如果问题有点宽泛我会道歉。

If this will be a public plugin, you won't be able to "bake in" the pages/posts on which it's supposed to show up. 如果这是一个公共插件,您将无法“烘焙”它应该显示的页面/帖子。

You'll either need to: 你要么需要:

1) Add an options page of some sort. 1)添加某种选项页面。 Take a look at The Option Page Docs for more information 有关更多信息,请查看选项页面文档

2) Add a Meta Box to the post types you want it to apply to that has an option in it that sets a meta value on the post. 2)将Meta Box添加到您希望它应用于的帖子类型中,其中有一个选项,用于在帖子上设置元值。

With the information provided, I'd lean towards #2, then you can just check if the meta value is set, and if so, display your plugin's code: 根据提供的信息,我倾向于#2,然后您可以检查是否设置了元值,如果是,则显示您的插件的代码:

if( is_single() && get_post_meta( get_the_ID(), 'run_amin_hakem_plugin', true ) == true ){
    // Do your thing        
}

You could then add the above code to an appropriate hook, like wp_head , wp_footer , or wherever your output is supposed to be displayed. 然后,您可以将上面的代码添加到适当的钩子中,例如wp_headwp_footer ,或者应该显示输出的位置。

There are multiple hooks that can help you with rendering the desired html/js. 有多个钩子可以帮助您渲染所需的html / js。 The very first that comes to mind are 首先想到的是

  1. wp_head
  2. wp_footer
  3. the_content
  4. the_title

If there is a sidebar you can add widget control plugin and show a widget on certain page. 如果有侧边栏,您可以添加窗口小部件控件插件并在特定页面上显示窗口小部件。

If you end up using any of the above mentioned hooks or any other hook, you can do conditional check using one of the following: 如果您最终使用上述任何钩子或任何其他钩子,您可以使用以下之一进行条件检查:

  1. is_page_template \\ Given that you know the custom template in use. is_page_template \\假设您知道正在使用的自定义模板。
  2. is_page \\ You will need to know page id/slug or title is_page \\您需要知道页面ID / slug或标题

Then there are many checks that will check if it is loading single.php or any of the taxonomy etc. 然后有许多检查将检查它是否正在加载single.php或任何分类法等。

Hope this answers your question. 希望这能回答你的问题。

This should be a good example of targeting based on post id 这应该是基于帖子ID定位的一个很好的例子

add_action( 'the_content', 'my_override_function' );
function my_override_function($content) {
    global $post;
    if ($post->ID == 123) {
        return 'Hello my friend<br>'.$content;
    } else {
        return $content;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找出更改页面中特定html内容的javascript代码的最佳方法是什么? - What's the best way to figure out which javascript code that change specific html content in page 处理 JavaScript 以允许用户访问 html 页面的最佳方法是什么? - What is the best way to handle a JavaScript to allow a user to access an html page? 禁用Wordpress上的javascript文件的最佳方法是什么? - What is the best way to disable a javascript files on Wordpress? 使用Javascript隐藏HTML页面上的按钮的最佳方法 - Best way to hide a button on an HTML page with Javascript 在HTML页面中使用JavaScript的最佳方法 - Best way to use JavaScript in a HTML page 在HTML页面中包含数据的最佳方法是什么? - What is the best way to include data in an HTML page? 我想在页面上加载多个图像时触发单个Javascript函数。 什么是最好的方式? - I want to trigger a single Javascript function when multiple images are done loading on the page. What will be the best way ? 用javascript页面加载后,过滤/更改某些内容的正确方法是什么? - what is the proper way to filter/change some content after a page loads with javascript? 在MVC中将PartialView呈现到布局页面的最佳方法是什么? - What is the best way to render PartialView to a Layout Page in MVC? 在页面加载时将服务器端HTML转换为Javascript MVC的最佳方法是什么? - What's the best way to convert server side HTML into a Javascript MVC on page load?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM