admin管理员组文章数量:1390256
I am writing my first humble plugin, which gets items from a simple database. For good SEO, I want to set the HTML title to be the item name, varying according to my users choice.
My user enters some choices on a form and I show results. Each item has one page and the URL gives me ?item=12345
so I can get the item name from my database. All this works.
I can successfully set the title, by setting up this in my top ("bootstrap"?) file of my plugin:
add_filter( 'pre_get_document_title', 'dwnz_filter_pagetitle', 20 );
to call my function (which queries the database for the item name).
My problem is: this filter is being invoked through the whole website, not just when my plugin is being used. How do I limit this call just to my plugin pages?
I am writing my first humble plugin, which gets items from a simple database. For good SEO, I want to set the HTML title to be the item name, varying according to my users choice.
My user enters some choices on a form and I show results. Each item has one page and the URL gives me ?item=12345
so I can get the item name from my database. All this works.
I can successfully set the title, by setting up this in my top ("bootstrap"?) file of my plugin:
add_filter( 'pre_get_document_title', 'dwnz_filter_pagetitle', 20 );
to call my function (which queries the database for the item name).
My problem is: this filter is being invoked through the whole website, not just when my plugin is being used. How do I limit this call just to my plugin pages?
Share Improve this question edited Mar 2, 2020 at 23:50 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Mar 2, 2020 at 23:46 Dave WilcoxDave Wilcox 1 2 |1 Answer
Reset to default 01) I think I solved it: check if the content contains my shortcode:
a) So set up in my top (outer) plugin file with:
add_filter( 'pre_get_document_title', 'dwnz_filter_pagetitle', 20 );
b) plus later on in same file:
function dwnz_filter_pagetitle ($title) {
// get_post even works in my case when I am expecting a page, not a post
$content = get_post();
if (has_shortcode($content->post_content, 'my_plugin_shortcode')) {
$new_page_title = get_something_from_my_database();
$page_title = $new_page_title;
return ($page_title);
}
}
2) Plus: I solved a similar problem: how do I ensure that my JS was enqueued only for my plugin content, (whereas I had it being enqueued on all output, unneccessary)
a) So set up in my top (outer) plugin file with:
add_shortcode('my_plugin_shortcode', 'my_top_function');
b) In same file:
function my_top_function($atts, $content = '', $tag){
$obj = new my_plugin_class ('my_title', 'my_version');
$html = $obj->some_method();
return ($html);
}
c) in my_plugin_class, in some_method, first do something that makes my_html and JS markup based on your inputs and logic. (I'm fetching from a database and making some google JS of marker locations). Then I queue one line for a normal reference to Google, plus another to append my dynamic JS to the WP queue, so that WP issues it in a standards-compliant way. The point being, all this only happens if your shortcode has invoked your code at the top.
$google_api_key = $this->get_google_api_key();
$google_include = 'https://maps.googleapis/maps/api/js?'
. 'v=3.exp&key='.$google_api_key;
$script_id = $this->plugin_name.'_googlemaps';
wp_enqueue_script( $script_id,
$google_include,
array( 'jquery' ), $this->version, true );
wp_add_inline_script($script_id,
$my_dynamic_js_string, 'after' );
....
return($my_html);
I hope this helps. I'm new to the etiquette here.
本文标签: filtersSetting the page title in a pluginbut not outside my plugin
版权声明:本文标题:filters - Setting the page title in a plugin, but not outside my plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744695605a2620258.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
dwnz_filter_pagetitle
you need to check what page you're on. If it's not one of yours, just pass the value through the filter unmodified, – Jacob Peattie Commented Mar 2, 2020 at 23:51