83 votes
Accepted

Enqueue Scripts / Styles when shortcode is present

I found an other way that works well for me: When initializing the plugin, do not enqueue your scripts and styles, but register them with wp_register_style and wp_register_script. Next you can load ...
jblanche's user avatar
  • 946
10 votes

Enqueue Scripts / Styles when shortcode is present

For my plugin I found that sometimes users have a theme builder that has shortcode stored in post meta data. Here is what I am using to detect whether my plugin shortcode is present in current post or ...
zdenekca's user avatar
  • 213
9 votes

Why isn't is_page working when I put it in the functions.php file?

get_header should work if you want to leave it in functions.php add_action('get_header', function() { if ( is_page( '2533' ) ) { // also tested with 'Apple' $bannerimg = 'apple....
Bryan Willis's user avatar
  • 4,289
6 votes

wp enqueue style on specific page templates

If the page template is located in a subdirectory of the theme (since WP 3.4), prepend the folder name and a slash to the template filename, e.g.: is_page_template( 'templates/about.php' ); So, ...
Razon Komar Pal's user avatar
6 votes
Accepted

Is_front_page inside header.php is always returning true

Yes it should work in the header file just like normal. Try using a standard IF statement instead of shorthand: if( is_front_page() ){ echo 'Front page!'; } You must have it configured in the ...
sMyles's user avatar
  • 2,783
5 votes

Enqueue Scripts / Styles when shortcode is present

WordPress has a built-in function to do something based on a specific Shortcode presenting status. The function name is has_shortcode(). You can use the following code to enqueue your style and ...
Arif Rahman's user avatar
5 votes

Can a conditional statement apply to part of a slug?

WordPress has no conditional for substring testing but you can do this using PHPs built-in function: strpos() global $post; if( false !== strpos( $post->post_name, 'beef' ) ) { // Do Things } ...
Howdy_McGee's user avatar
  • 20.3k
5 votes

Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option

You're in luck. I was actually looking for an excuse to do this on one of my projects. I finally took the time to add this idea into one of my themes. :) You can take this idea and run with it. It's ...
Michael Ecklund's user avatar
4 votes

wp enqueue style on specific page templates

I don't know if the solutions provided in other answers used to work, but (since there's no accepted answer!) it seems the correct answer is currently: function my_enqueue_stuff() { if ( ...
richplane's user avatar
  • 141
4 votes

Enqueue Scripts / Styles when shortcode is present

I do so: class My_Shortcode { function __construct() { do_action('my_start_shortcode'); // call .... and catch hook in other functions (or other plugins): function ...
Vova Druzhaev's user avatar
4 votes

If post author role is X

I think you're going about it the right way. The only thing I'd do differently is to separate the presentation from the logic. So you'd have: in the template file: <div class="<?php echo ...
Nathan Johnson's user avatar
4 votes
Accepted

If post author role is X

One way to do it is getting the author data and checking it's role with get_userdata() Codex For that you will need the user ID, and you can get that with get_post_field() Codex You will need the ...
Marcelo Henriques Cortez's user avatar
3 votes
Accepted

Alternate text if shortcode returns no data

You should do this in the shortcode/plugin itself. An easy way to do it may be something like this: // if the shortcodes empty: if ( empty( $shortcode_content ) ) { // set a default nothing found ...
Tom J Nowell's user avatar
  • 56.5k
3 votes

Enqueue Scripts / Styles when shortcode is present

As someone mentioned using 'has_shortcode' will do it. Here's the example I found very helpful (Source: Wordpress.org) function wpdocs_shortcode_scripts() { global $post; if ( is_a( $post, '...
Simon's user avatar
  • 145
3 votes
Accepted

Changing select options based on previous select with PHP & AJAX

The first step you'll need to take is to reorganize your code a little. WordPress allows you to register Ajax calls via an action, wp_ajax. This is a safer and easier way to create an ajax call that ...
Tom's user avatar
  • 1,286
3 votes

Conditional IF current cat is the smallest/last/end child cat THEN

You can check if the current term has children. If it does, it's not the lowest, if it doesn't, it's the lowest of that branch. if( is_category() ){ $children = get_terms( [ 'taxonomy' =&...
Milo's user avatar
  • 77.6k
3 votes
Accepted

Adding a template part as part of a shortcode

get_template_part doesn't return its value, it outputs it, which is how it would normally be used. Think of it this way, how would it know wether to echo the template or return it? So instead, we use ...
Tom J Nowell's user avatar
  • 56.5k
2 votes

Loading scripts only if a particular shortcode or widget is present

with Wordpress 4.7 there is another way to achieve this in your functions.php file, add_action( 'wp_enqueue_scripts', 'register_my_script'); function register_my_script(){ wp_register_script('my-...
Aurovrata's user avatar
  • 1,267
2 votes

Nested conditionals

Write it out in english and be clear about your logic, aka if you say that X should happen when Y is true, what happens if Y is false? AKA else. If X then Y, else Z If we're on the home page, ...
Tom J Nowell's user avatar
  • 56.5k
2 votes

Not able to see the error

Isn't it the one just above <span id="max-pages" style="display:none"> ?
Andy Macaulay-Brook's user avatar
2 votes

Not able to see the error

You have a lone endif at the end of your pagination block and then you forgot the ; here: <span id="max-pages" style="display:none"><?php echo $max_pages ?></span>
Mateusz Hajdziony's user avatar
2 votes

Advanced Custom Fields - display label and value only if value entered

You have a mismatch in your PHP tags. Use this: <?php $field_name = "text_field"; $field = get_field_object($field_name); ?> <table> <tbody> <?php if( isset($...
jdm2112's user avatar
  • 3,645
2 votes
Accepted

Conditional does not work with add_filter

is_page() requires WP_Query to be initialized and I assume your code runs before this. You can use this function within the filter callback instead: add_filter('the_content', function($c){ return ...
Ismail's user avatar
  • 1,916
2 votes

Can a conditional statement apply to part of a slug?

In a similar way as @Howdy_McGee, we might try to construct an helper function: /** * @param $s String * @return bool */ function queried_post_name_contains_wpse247782( $s ) { $obj = ...
birgire's user avatar
  • 65.9k
2 votes
Accepted

Which is the better way to write a conditional statement?

As far as I can tell, there's no performance difference. And both styles are acceptable by WordPress Coding Standards as you can read in: Braces should always be used, even when they are not ...
Celso Bessa's user avatar
  • 1,088
2 votes

BuddyPress User Profile Menu

I solved it with my own effort. function bp_remove_nav_item() { global $bp; $current_user = wp_get_current_user(); $role_name = $current_user->roles[0]; if($role_name==='...
083N's user avatar
  • 31
2 votes
Accepted

Display post if a specific tag is present

According to the docs the input parameters can be: has_tag( string|int|array $tag = '', int|object $post = null ) so you could try e.g. if( has_tag( 'test' ) ) { ... } to check if the current post ...
birgire's user avatar
  • 65.9k
2 votes
Accepted

Conditional: IF current user has NOT commented on current post (not including post author)

[EDIT] Here is the modified code: $post_id = get_the_ID(); // ID of the current post. $user_id = get_current_user_id(); // ID of the current user. $post_author_id = get_post_field( '...
Sally CJ's user avatar
  • 36k
2 votes

Conditional: IF post creation date is in the first half OR last half of the year

Are you thinking of something like this? if ( have_posts() ) { while ( have_posts() ) { the_post(); if ( absint( get_the_date( 'n' ) ) <= 6 ) { // First half of ...
windyjonas's user avatar
2 votes
Accepted

How to check if the current page is at a specified path in the URL?

You could try this instead if (!is_front_page() && is_home()) { get_template_part('template-parts/blog-categories'); } Explanation for the difference between front_page an home is here: ...
user203767's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible