76 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 ...
user avatar
  • 876
31 votes

Enqueue Scripts / Styles when shortcode is present

Before starting answer I have to say that regarding this topic css and js are not the same. The reason is simple: while adding js to the body of the page (in footer) is a common and valid way to go, ...
user avatar
  • 44.6k
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 ...
user avatar
  • 213
8 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....
user avatar
  • 4,129
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 ...
user avatar
  • 2,683
5 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, ...
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 } ...
user avatar
  • 19.6k
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 ...
user avatar
4 votes
Accepted

Query posts by content lenght

I want to suggest you another approach. You can set a meta when you saving a post if its content is less than 140 chars, so then you can simply runs a simple meta query. add_action( 'save_post_post',...
user avatar
  • 44.6k
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 ...
user avatar
4 votes
Accepted

Wordpress equivalent to Drupal Views?

Its not identical to Drupal Views, but i think WP Views is the closest you will get to Drupal Views. I am a Drupal guy, and i use WP Views for every Wordpress project i create.
user avatar
  • 110
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 ...
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 ...
user avatar
3 votes

is_main_site() Always Returns True

I am not calling it inside of a hook but just straight away as the first thing in a MU plugin. At that point all the info you are requesting won't be available. That's running before any hook and ...
user avatar
  • 49.8k
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, '...
user avatar
  • 145
3 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 ...
user avatar
3 votes

PHP conditional script issue

You have a lot of PHP tag spam, e.g.: ?><?php This is bad, confusing, makes your code difficult to read, and a waste of your time to type. It's the difficulty reading your code that has lead ...
user avatar
  • 51.7k
3 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 ( ...
user avatar
  • 131
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 ...
user avatar
  • 51.7k
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 ...
user avatar
  • 1,256
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' =&...
user avatar
  • 76k
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 ...
user avatar
  • 51.7k
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-...
user avatar
  • 1,158
2 votes
Accepted

If Specific Post, Link Elsewhere

The reason why your check didn’t work is because is_singular() checks for post types, not for post IDs. is_singular( 'product' ) checks if the current post is of the type product, nothing else. The ...
user avatar
  • 105k
2 votes
Accepted

Conditional for 404 page not work

You should wrap your enqueue/register function in your conditional, not your add_action() You can try something like this using the is_404() conditional check function enqueue_404_script() { if(...
user avatar
2 votes

Display several random posts, but make sure a condition is met

I made a few adjustments. Once I created my two arrays, I combined them this way $merged_queries->posts = array_merge( $women->posts, $men->posts ); shuffle($merged_queries->posts); $...
user avatar
2 votes
Accepted

Display several random posts, but make sure a condition is met

I think the best way here would be to run three separate loops here (you can also do two loops, but then you need to get full posts in the men and women queries, combine them and shuffle them), the ...
user avatar
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, ...
user avatar
  • 51.7k
2 votes

Not able to see the error

Isn't it the one just above <span id="max-pages" style="display:none"> ?
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>
user avatar

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