Execute Only for Specific Post Categories
-
I’m using <?php echo do_shortcode(‘[adblockingdetector id=”14″]’); ?> — as given on your own website for this plugin — in a theme template file and was wondering how (not knowing PHP at all) that code might be easily made to quickly check for specific Post IDs, determining whether Ad Blocking Detector would run at all….
Is there a simple way, in the PHP, to first check for a certain category of post before implementing Ad Blocking Detector? IOW, how can Ad Blocking Detector be prevented from working at all, given certain post categories??
-
Basically, something like
function ToBeOrNotToBe() {
if category-id = array (a, b, c)
then No-Ad-Blocking-Detector-Please
else echo do_shortcode(‘[adblockingdetector id=”14″]’);
}THANKS!!!
Well, never mind: I guess when given enough time, I’ll eventually find a workaround!
“Sorry for the bother,” as I’m wont to say these days….
(But please do take a look at my new thread, which is related to this one: “Plugin Doesn’t Like My Page” — thanks!)
Sorry for the response delay. I was in class all day yesterday, and extremely busy all day today. Just now stopped running around.
It looks like you’ve found a solution based on the other thread (I’ll go there and discuss that once finished here). But, I’ll answer the original question anyway in case someone else is interested.
WordPress offers a couple PHP functions that can help with identifying categories of certain posts. For starters:
Which function you should use and how you use it will depend on the context and precisely what you want to do.
WordPress themes use a concept the developers call “The Loop“. When inside The Loop, certain functions become available, and certain parameters of other functions can be omitted. Inside The Loop, it is fairly easy to accomplish what you want. Outside The Loop, it is much more difficult.
If you are doing this do_shortcode() call inside The Loop, then you can simply use the in_category function passing it the category ID as a parameter.
Here is a rough function based on yours that accomplishes what you are looking for. The code is commented using PHP comment notation to explain what’s happening (“// the comment goes here”).
function to_be_or_not_to_be() { // An arrray of category IDs you want to skip. // If you know these, simply replace the numbers with // the category IDs. If you need to find the IDs, you can // use WordPress functions like get_category_by_slug() $category_ids_to_skip = array( 1, 2, 3 ); // Loop through the array and check if current post // has that ID. foreach( $category_ids_to_skip as $category_id ) { if( in_category( $category_id ) ) { // Uh oh! We have a match. One of the IDs // specified matches this posts' categories. Do // whatever we are supposed to do when category // matches. If nothing, simply return. return; } } // If we reach this point, then all category IDs specified // have been looped through and none matched. Therefore, we // can display the shortcode. echo do_shortcode('[adblockingdetector id="14"]'); } // end function to_be_or_not_to_be()
If you are not in The Loop, then life becomes much more difficult and highly dependent on what you are trying to accomplish. If this is the case, your other method might be the better choice. Let me know if you want more information about this.
Now, I’ll go to the other thread and discuss that.
Hi, John,
Thanks so much for taking the time to respond — and in such detail, too!
The “concept code” (“code mockup” or whatever it’s called) you provided is exactly what I had in mind. In particular,
foreach( $category_ids_to_skip as $category_id ) {
if( in_category( $category_id ) ) {
// Uh oh! We have a match. One of the IDs
// specified matches this posts’ categories. Do
// whatever we are supposed to do when category
// matches.caught my attention…now I really do I know better, but I’m gonna ask anyway, mostly out of sheer curiosity: what would the PHP be for “Uh oh! We have a match. One of the IDs specified matches this post’s categories. Do NOT output Ad Blocker Detected shortcode“???
Now, John, I’ve been trying to make this thread as general as possible to possibly be of some benefit to others as well (and kudos for taking the initiative to respond at length for the reference of posterity!) but I think I’ll have to get “personal” here so that the context of my request makes better sense, as it sounds pretty ridiculous that I want to disable your plugin without actually disabling it!
(The following is actually not all that important but provided in case it matters somehow.)
See, the thing is, for certain Posts, I don’t want a particular ad unit showing (the upper left-hand corner one, to be exact). So I’ve got CSS to target it for specific Post Categories and specific Post IDs.
But here’s the tricky thing: targeting
.ABD_display
works when there’s no ad-blocker enabled (so that I can suppress display of that particular upper left-hand corner ad unit), but when an ad-blocker is enabled, targeting.ABD_display_adblocker
makes all ads disappear!Hence why I thought to abandon CSS and turn to PHP….
The function I posted before does not insert the Ad Blocker Detected shortcode when the categories match. It only inserts if there is no match.
The foreach loop systematically loops through the array of IDs. For each ID in the array $categories_ids_to_skip, it checks if the current post belongs to that category. If it finds a matching category, the function executes the PHP keyword return. The way I used it, this return stops the function in its tracks and leaves it. Effectively, the return prevents what is after the foreach from running.
However, if the foreach runs out of $category_ids_to_skip and hasn’t found a match, then the return is never reached and the function is allowed to continue past the foreach. At the end of the function, is the PHP to insert the shortcode.
So, in simpler form, this is the logic:
- Assign the categories to skip to a variable. — $category_ids_to_skip = array( 1, 2, 3 );
- Loop through the list of categories to skip. — foreach( $category_ids_to_skip as $category_id ) {
- If the category matches at any time during the looping, stop right here and ignore the rest of this list. Effectively, do nothing more. — if( in_category( $category_id ) ) { return; }
- Otherwise, continue the looping until we have checked all categories in the list, and move on down this list.
- Now that the loop is done, if we reach this point, none of the categories match. So insert the shortcode. — echo do_shortcode(‘[adblockingdetector id=”14″]’);
———————-
While doable, reversing this logic so the if() checks the opposite is more difficult. Using a list of category IDs, and the WordPress function in_category(), kind of locks us into a certain way of approaching the problem. It is doable, but, in programming parlance, would be inelegant. If you really want me to, I can come up with a way of doing it fairly quickly (just let me know).
———————-
I do however understand that you’re probably not using a function in your template, and instead want code that can just be dropped in place. For that, you can use a slight modification of the original code I gave. Just drop this where you would have put the <?php echo do_shortcode(‘[adblockingdetector id=”14″]’); ?> (after changing the post category IDs) and it should work.
<?php // An arrray of category IDs you want to skip. // If you know these, simply replace the numbers with // the category IDs. If you need to find the IDs, you can // use WordPress functions like get_category_by_slug() $category_ids_to_skip = array( 1, 2, 3 ); // Let's create and ready a flag to determine whether we // insert the shortcode or not. It starts off as true, // and will be changed to false if categories match later. $insert_the_shortcode = true; // Loop through the array and check if current post // has that ID. foreach( $category_ids_to_skip as $category_id ) { if( in_category( $category_id ) ) { // Uh oh! We have a match. One of the IDs // specified matches this posts' categories. Do // whatever we are supposed to do when category // matches. In this case, that is change the flag $insert_the_shortcode = false; } } // If we reach this point, then all category IDs specified // have been looped through, and the $insert_the_shortcode // flag can tell us whether to display the shortcode or not. if( $insert_the_shortcode ) { echo do_shortcode('[adblockingdetector id="14"]'); } ?>
This is very similar to the original code, but instead of relying on the return keyword to abandon a function, we instead create a flag that tells us whether it’s okay to insert the shortcode or not.
—————
I’ll address the CSS in the other thread.
Actually, I do fiddle with my WordPress templates — child theme templates, that is — but a CSS option would be much more preferable, I think.
I’m gonna have fun this weekend trying out the various methods you’ve so graciously provided…I’m truly grateful: please check your e-mail…. ??
Hello Again, John,
This method turns out to work better than the CSS-based one (not too surprisingly, in a way, as what I’m really trying to do here is to control behavior, not layout as such) over on the other thread…just one thing more now: what’s the PHP for the equivalent of your
if( in_category( $category_id ) ) {
…only for individual Post IDs??
I’d like to also target posts as well as post categories. (Long story. ;-))
It couldn’t be as simple as
if( in_post( $post_id ) ) {
— could it???
Oh, also, just for possible future reference, how could individual pages be targeted?
if( in_page( $page_id ) ) {
?
Behind the scenes in WordPress, pages and posts largely aren’t treated as separate items. They each have a unique ID that is checked the same way.
To check, you can use the WordPress function get_the_ID(). The if statement equivalent would be this:
if( $post_id == get_the_ID() ) {
Simply place the IDs of pages and posts you want to block in an array variable like $category_ids_to_skip, I’d call it $post_ids_to_skip. Then, replace $category_ids_to_skip in the foreach loop and change the if statement to the above.
Giving something like this:
<?php // An array of post and page IDs you want to skip. // If you know these, simply replace the numbers with // the category IDs. If you need to find the IDs, you can // use WordPress functions like get_category_by_slug() $post_ids_to_skip = array( 1, 2, 3 ); // Let's create and ready a flag to determine whether we // insert the shortcode or not. It starts off as true, // and will be changed to false if categories match later. $insert_the_shortcode = true; // Loop through the array and check if current post // has that ID. foreach( $post_ids_to_skip as $post_id ) { if( $post_id == get_the_ID() ) { // Uh oh! We have a match. One of the IDs // specified matches this posts'/pages' ID. Do // whatever we are supposed to do when IDs // match. In this case, that is change the flag $insert_the_shortcode = false; } } // If we reach this point, then all category IDs specified // have been looped through, and the $insert_the_shortcode // flag can tell us whether to display the shortcode or not. if( $insert_the_shortcode ) { echo do_shortcode('[adblockingdetector id="14"]'); } ?>
Beautiful. Simply beautiful.
Thanks, John — this is a great little bit of code I’m going to try out with other plugins, too, whenever I need similarly fine-grained control over the circumstances of their display!! I daresay you’ve helped many, many people beyond myself.
- The topic ‘Execute Only for Specific Post Categories’ is closed to new replies.