|
View:
New views
20 Messages
—
Rating Filter:
Alert me
|
|
|
checking if post has shortcode, before header is output?Hello,
Is there anyway to check if there your registered shortcode (for a plugin) is being used in the current post, during some action hook before the header is output? The idea would be to include CSS and JS files for the shortcode, only on pages that actually use the shortcode. I'm doing it inline right now, since wp_enqueue_script is not available where the shortcode is dealt with. Thanks, Kevin N. _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?Try this:
add_action('template_redirect', 'add_scripts'); function add_scripts() { if ( ! is_page() ) return; global $posts; if ( FALSE === strpos($posts[0]->post_content, '[your_shortcode') ) return; wp_enqueue_script(... } -- http://scribu.net _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?This is not an answer but a praise to you for thinking this way.
Too many plugins require_once and wp_enqueue_script their entire machinery at init or at wp_head without first checking whether the page actually needs them. 2009/10/23 Kevin Newman <CaptainN@...>: > Hello, > > Is there anyway to check if there your registered shortcode (for a plugin) > is being used in the current post, during some action hook before the header > is output? The idea would be to include CSS and JS files for the shortcode, > only on pages that actually use the shortcode. I'm doing it inline right > now, since wp_enqueue_script is not available where the shortcode is dealt > with. > > Thanks, > > Kevin N. > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?That won't work for any custom queries (as well as a few other edge
cases like [[your_shortcode), but it'll work for most scenarios. You could also have your shortcode handler add an action that hooks into wp_footer and uses wp_print_scripts() to load your js file. You get the benefit of the js loading in the footer as well as the functionality you wanted of only loading your js when needed. scribu wrote: > Try this: > > add_action('template_redirect', 'add_scripts'); > > function add_scripts() { > if ( ! is_page() ) > return; > > global $posts; > > if ( FALSE === strpos($posts[0]->post_content, '[your_shortcode') ) > return; > > wp_enqueue_script(... > } > > > _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On Fri, Oct 23, 2009 at 6:02 PM, scribu <scribu@...> wrote:
> Try this: > > add_action('template_redirect', 'add_scripts'); > > function add_scripts() { > if ( ! is_page() ) > return; > > global $posts; > > if ( FALSE === strpos($posts[0]->post_content, '[your_shortcode') ) > return; Rather than checking the global $posts variable, I'd recommend using the WP_Query API: add_action('template_redirect', 'add_scripts'); function add_scripts() { global $wp_query; if ( is_singular() ) { $post = $wp_query->get_queried_object(); if ( false !== strpos($post->post_content, '[your_shortcode') ) { wp_enqueue_script(... } } } _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On Sat, Oct 24, 2009 at 9:02 PM, Aaron D. Campbell <aaron@...> wrote:
> That won't work for any custom queries (as well as a few other edge cases > like [[your_shortcode), but it'll work for most scenarios. You could also > have your shortcode handler add an action that hooks into wp_footer and uses > wp_print_scripts() to load your js file. You get the benefit of the js > loading in the footer as well as the functionality you wanted of only > loading your js when needed. To prevent the edge cases you can use get_shortcode_regex() to know that you are checking for the shortcode the same way wp is. Something like this: $pattern = get_shortcode_regex(); preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches); if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') { //shortcode is being used } -- Chris Scott http://iamzed.com/ http://hailtheale.com/ > > > scribu wrote: >> >> Try this: >> >> add_action('template_redirect', 'add_scripts'); >> >> function add_scripts() { >> if ( ! is_page() ) >> return; >> >> global $posts; >> >> if ( FALSE === strpos($posts[0]->post_content, '[your_shortcode') ) >> return; >> >> wp_enqueue_script(... >> } >> >> >> > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?That still doesn't fix the case where more than one query is ran on a
page, when someone does something like "$myQuery = new WP_Query($params);" you'd process the main query, but you wouldn't get any of the shortcodes in the custom query. Chris Scott wrote: > On Sat, Oct 24, 2009 at 9:02 PM, Aaron D. Campbell <aaron@...> wrote: > >> That won't work for any custom queries (as well as a few other edge cases >> like [[your_shortcode), but it'll work for most scenarios. You could also >> have your shortcode handler add an action that hooks into wp_footer and uses >> wp_print_scripts() to load your js file. You get the benefit of the js >> loading in the footer as well as the functionality you wanted of only >> loading your js when needed. >> > > To prevent the edge cases you can use get_shortcode_regex() to know > that you are checking for the shortcode the same way wp is. Something > like this: > > $pattern = get_shortcode_regex(); > preg_match('/'.$pattern.'/s', $posts[0]->post_content, $matches); > if (is_array($matches) && $matches[2] == 'YOURSHORTCODE') { > //shortcode is being used > } > _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On Mon, Oct 26, 2009 at 1:41 PM, Aaron D. Campbell <aaron@...> wrote:
> That still doesn't fix the case where more than one query is ran on a page, > when someone does something like "$myQuery = new WP_Query($params);" you'd > process the main query, but you wouldn't get any of the shortcodes in the > custom query. That's a logical point but I suspect that in the majority of cases people are using extra queries to show only parts of other content, headlines or excerpts, rather than the actual the_content(). In these cases checking the extra queries will actually cause the js to be loaded way more often than desired. If 1/5 articles needs the JS and you have 10 "related" headlines visible in each single.php view then the likelihood that one of the headlines is a post that *would* need the js is very high, and you'll be constantly loading it even though the headlines have no use for it. If you do show extra loops with full content (or you need this to work on indexes/archives that show multiple posts with full content) then +1 for the idea of adding the js to the footer. I believe there's a hook on 'the_post' that would work pretty well to identify posts that get displayed, though if its not too late the_content() would be ideal, as it would only account for posts who's full content ( and thus expanded shortcodes ) are displayed. -- Jeremy Clarke | http://jeremyclarke.org Code and Design | globalvoicesonline.org _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On Mon, Oct 26, 2009 at 12:41 PM, Aaron D. Campbell <aaron@...> wrote:
> That still doesn't fix the case where more than one query is ran on a page, > when someone does something like "$myQuery = new WP_Query($params);" you'd > process the main query, but you wouldn't get any of the shortcodes in the > custom query. That's because that is not possible. You're basically wanting to know the content returned by the query before you ever run the query itself. Can't be done, because of, you know, time only traveling in one direction. Damn physicists. To do this sort of thing, you'd need to use output buffering. Buffer the whole page, then modify it after the fact, inserting what you want where you want. -Otto _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?I still think it would be better to add the JS from the shortcode
handler rather than trying to mess with the_content, the_post, $wp_query, $post, etc. Since it's enqueued inside the shortcode handler you KNOW the shortcode is being executed. The only drawback is that the JS needs to go in the footer rather than the header, but as far as I know this is actually the recommended way of loading JS so you should code for that anyway. Jeremy Clarke wrote: > On Mon, Oct 26, 2009 at 1:41 PM, Aaron D. Campbell <aaron@...> wrote: > >> That still doesn't fix the case where more than one query is ran on a page, >> when someone does something like "$myQuery = new WP_Query($params);" you'd >> process the main query, but you wouldn't get any of the shortcodes in the >> custom query. >> > > That's a logical point but I suspect that in the majority of cases > people are using extra queries to show only parts of other content, > headlines or excerpts, rather than the actual the_content(). In these > cases checking the extra queries will actually cause the js to be > loaded way more often than desired. If 1/5 articles needs the JS and > you have 10 "related" headlines visible in each single.php view then > the likelihood that one of the headlines is a post that *would* need > the js is very high, and you'll be constantly loading it even though > the headlines have no use for it. > > If you do show extra loops with full content (or you need this to work > on indexes/archives that show multiple posts with full content) then > +1 for the idea of adding the js to the footer. I believe there's a > hook on 'the_post' that would work pretty well to identify posts that > get displayed, though if its not too late the_content() would be > ideal, as it would only account for posts who's full content ( and > thus expanded shortcodes ) are displayed. > _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On Mon, Oct 26, 2009 at 1:59 PM, Aaron D. Campbell <aaron@...> wrote:
> I still think it would be better to add the JS from the shortcode handler > rather than trying to mess with the_content, the_post, $wp_query, $post, > etc. Since it's enqueued inside the shortcode handler you KNOW the > shortcode is being executed. The only drawback is that the JS needs to go > in the footer rather than the header, but as far as I know this is actually > the recommended way of loading JS so you should code for that anyway. That's probably the best way for JavaScript, but valid CSS <style> elements are supposed to be in <head>. _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?Basically you have to predict at wp_head which posts will have their
the_content displayed on the page. As others said, at the time of wp_head you cannot know what part in the page will display content that may contain that shortcode. The 'template_redirect' suggestion doesn't cover archive pages and the blog front page. To really cover all bases, you will need to generate the whole page unto a buffer first to find out, and if it does, insert your script and css. Another inelegant solution is to store all the possible REQUEST_URIs that will have the shortcode into a table, and at wp_head use this table to decide. Or lastly, just give this up, and insert your CSS (invalid) and JS (okay) in the footer. Strictly speaking, stylesheets and <style> tags must be in the <head> tag. As I said, I don't have the answer for this. 2009/10/23 Kevin Newman <CaptainN@...>: > Hello, > > Is there anyway to check if there your registered shortcode (for a plugin) > is being used in the current post, during some action hook before the header > is output? The idea would be to include CSS and JS files for the shortcode, > only on pages that actually use the shortcode. I'm doing it inline right > now, since wp_enqueue_script is not available where the shortcode is dealt > with. > > Thanks, > > Kevin N. > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On Mon, Oct 26, 2009 at 2:29 PM, Otto <otto@...> wrote:
> > That's because that is not possible. You're basically wanting to know > the content returned by the query before you ever run the query > itself. Can't be done, because of, you know, time only traveling in > one direction. Damn physicists. > > I anxiously await the Higgs Boson plugin for PHP/MySQL, as this will solve a GREAT many problems. -- -Doug http://literalbarrage.org/blog/ _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?That's true, and the best I can come up with for that is to do the CSS
inline or put the <style> element in the body by outputting it from your shortcode handler (keeping track of when you output it so you don't output it more than once). Austin Matzko wrote: > That's probably the best way for JavaScript, but valid CSS <style> > elements are supposed to be in <head> _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?That's true, which is why I was advocating something like this:
function myplugin_add_js () { wp_print_scripts( 'myplugin_myjs' ); } function myplugin_shortcode_handler($attr, $content = '') { add_action( 'wp_footer', 'myplugin_add_js' ); } That's assuming that your js is already registered using the handle 'myplugin_myjs' and that the wp_footer action is correctly called from your theme. Otto wrote: > On Mon, Oct 26, 2009 at 12:41 PM, Aaron D. Campbell <aaron@...> wrote: > >> That still doesn't fix the case where more than one query is ran on a page, >> when someone does something like "$myQuery = new WP_Query($params);" you'd >> process the main query, but you wouldn't get any of the shortcodes in the >> custom query. >> > > That's because that is not possible. You're basically wanting to know > the content returned by the query before you ever run the query > itself. Can't be done, because of, you know, time only traveling in > one direction. Damn physicists. > > To do this sort of thing, you'd need to use output buffering. Buffer > the whole page, then modify it after the fact, inserting what you want > where you want. > > -Otto wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?For valid CSS, f you're not worried about accessibility just add
additional the stylesheet with jQuery on page load if it meets some sort of condition that you place in the source (put the JS in the footer like the others have suggested). Glenn Ansley http://fullthrottledevelopment.com On Mon, Oct 26, 2009 at 3:13 PM, Aaron D. Campbell <aaron@...> wrote: > That's true, and the best I can come up with for that is to do the CSS > inline or put the <style> element in the body by outputting it from your > shortcode handler (keeping track of when you output it so you don't output > it more than once). > > Austin Matzko wrote: >> >> That's probably the best way for JavaScript, but valid CSS <style> >> elements are supposed to be in <head> > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers > wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?In HTML 5, the <style> element being using in the body is valid if you use
the "scoped" attribute :) http://www.w3schools.com/html5/tag_style.asp -------------------------------------------------- From: "Kevin Newman" <CaptainN@...> > Hello, > > Is there anyway to check if there your registered shortcode (for a plugin) > is being used in the current post, during some action hook before the > header is output? The idea would be to include CSS and JS files for the > shortcode, only on pages that actually use the shortcode. I'm doing it > inline right now, since wp_enqueue_script is not available where the > shortcode is dealt with. > > Thanks, > > Kevin N. > _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?On 10/26/09 3:12 PM, Doug Stewart wrote:
> On Mon, Oct 26, 2009 at 2:29 PM, Otto<otto@...> wrote: > > >> That's because that is not possible. You're basically wanting to know >> the content returned by the query before you ever run the query >> itself. Can't be done, because of, you know, time only traveling in >> one direction. Damn physicists. >> >> >> > I anxiously await the Higgs Boson plugin for PHP/MySQL, as this will solve a > GREAT many problems. > > _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?This is almost what I ended up doing. Outputting the <style> tag might
violate some standard, but it does work, so I'll keep it that way - and adjust for HTML5 later (thanks Ken!). I'm inline outputting the scripts too, but moving those to the footer sounds like a good idea. Getting any of this into the header on only the right pages sounds exceedingly difficult, and costly in terms of necessary code bloat, for little (any?) benefit. Thanks for all the suggestions. Kevin N. On 10/26/09 2:59 PM, Aaron D. Campbell wrote: > I still think it would be better to add the JS from the shortcode > handler rather than trying to mess with the_content, the_post, > $wp_query, $post, etc. Since it's enqueued inside the shortcode > handler you KNOW the shortcode is being executed. The only drawback > is that the JS needs to go in the footer rather than the header, but > as far as I know this is actually the recommended way of loading JS so > you should code for that anyway. > > Jeremy Clarke wrote: >> On Mon, Oct 26, 2009 at 1:41 PM, Aaron D. Campbell >> <aaron@...> wrote: >>> That still doesn't fix the case where more than one query is ran on >>> a page, >>> when someone does something like "$myQuery = new WP_Query($params);" >>> you'd >>> process the main query, but you wouldn't get any of the shortcodes >>> in the >>> custom query. >> >> That's a logical point but I suspect that in the majority of cases >> people are using extra queries to show only parts of other content, >> headlines or excerpts, rather than the actual the_content(). In these >> cases checking the extra queries will actually cause the js to be >> loaded way more often than desired. If 1/5 articles needs the JS and >> you have 10 "related" headlines visible in each single.php view then >> the likelihood that one of the headlines is a post that *would* need >> the js is very high, and you'll be constantly loading it even though >> the headlines have no use for it. >> >> If you do show extra loops with full content (or you need this to work >> on indexes/archives that show multiple posts with full content) then >> +1 for the idea of adding the js to the footer. I believe there's a >> hook on 'the_post' that would work pretty well to identify posts that >> get displayed, though if its not too late the_content() would be >> ideal, as it would only account for posts who's full content ( and >> thus expanded shortcodes ) are displayed. > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
|
|
Re: checking if post has shortcode, before header is output?I ended up putting it in the body. This just makes too much sense (and
is easy, compared with everything else, which is hard). Kevin N. On 10/26/09 4:30 PM, Ken Newman wrote: > In HTML 5, the <style> element being using in the body is valid if you > use the "scoped" attribute :) > http://www.w3schools.com/html5/tag_style.asp > > -------------------------------------------------- > From: "Kevin Newman" <CaptainN@...> > >> Hello, >> >> Is there anyway to check if there your registered shortcode (for a >> plugin) is being used in the current post, during some action hook >> before the header is output? The idea would be to include CSS and JS >> files for the shortcode, only on pages that actually use the >> shortcode. I'm doing it inline right now, since wp_enqueue_script is >> not available where the shortcode is dealt with. >> >> Thanks, >> >> Kevin N. >> > > _______________________________________________ > wp-hackers mailing list > wp-hackers@... > http://lists.automattic.com/mailman/listinfo/wp-hackers _______________________________________________ wp-hackers mailing list wp-hackers@... http://lists.automattic.com/mailman/listinfo/wp-hackers |
| Free embeddable forum powered by Nabble | Forum Help |