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 ...
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 ...
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....
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, ...
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 ...
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 ...
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
}
...
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 ...
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 ( ...
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 ...
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 ...
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 ...
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 ...
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, '...
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 ...
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' =&...
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 ...
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-...
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,
...
2
votes
Not able to see the error
Isn't it the one just above <span id="max-pages" style="display:none"> ?
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>
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($...
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 ...
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 = ...
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 ...
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==='...
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 ...
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( '...
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 ...
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: ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
conditional-content × 278php × 44
conditional-tags × 28
custom-post-types × 19
functions × 17
loop × 17
custom-field × 15
shortcode × 14
categories × 12
theme-development × 12
plugins × 10
posts × 10
pages × 10
wp-query × 9
customization × 8
custom-taxonomy × 8
menus × 8
javascript × 7
widgets × 7
advanced-custom-fields × 7
images × 6
filters × 6
user-roles × 6
page-template × 6
sidebar × 6