You should wrap your condition inside the function, rather than the function inside the condition (this will also prevent errors with code running too early as WordPress loads).
function my_admin_notice() {
global $wpdb, $post;
$depth = $wpdb->get_var(
$wpdb->prepare( "SELECT post_parent FROM $wpdb->posts WHERE ID = %d", $post->ID )
);
if ( $depth == '0' ) : ?>
<div class="updated">
<p><?php _e( 'Depth is 0!', 'my-text-domain' ); ?></p>
</div>
<?php
endif;
}
add_action( 'admin_notices', 'my_admin_notice' );
Related Posts:
- Functions file mods and CPU
- site_url is not honoring scheme
- Remove theme, change theme button and WP version on “Right Now” admin dashboard?
- WPDB function not saving
- Making an under maintenance page (without using plugins)
- Why wp_ajax hooks doesn’t work?
- Two ‘If Statements’ is my syntax correct (functions.php) and what is ‘false’
- Should I use `get_stylesheet_uri()` or `get_template_directory_uri()` when calling my CSS Stylesheet?
- Remove snippets of JS from core
- Sort results by name & asc order on homepage
- Add the title of a widget as an ID – for anchor links
- Additional folder where will Media Library look for files
- Do something when user creates post (that’s pending)
- Why my wp_enqueue_script doesnt work on some page?
- WordPress SEO By Yoast Plugin is interfering with a custom function code – how to troubleshoot and fix it?
- Dynamic body classes based on current page
- Hiding Google Analytics code based on theme options
- Shortcodes not outputting in correct divs
- Button generate a random URL [closed]
- Why don’t some template tags work when querying information on single.php?
- How do I add e-mail subscription functionality
- How to Delete Posts by title?
- shortcode inside post called by ID does not render as expected
- Problem with Child-Theme using of foundation-framework [closed]
- Code in functions.php appearing on front-end and dashboard [closed]
- get_footer can’t find any variables set in functions.php
- How do I access variables outside a function [closed]
- clickable title of 2 post on mainpage USING functions.php
- Excerpt – First Sentence & Read More
- wp_get_current_user->user login returns %20 for spaces
- Disable every rss feed except home feed
- Notice: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ in ….functions.php on line 73
- How to test for a class (from a plugin) in functions.php
- If two tags in the middle i need comma
- How can I append and prepend something to all post hyperlinks without using ::before or ::after? PHP hook solution?
- Child Theme not working – CSS gone
- Show stuff everywhere except single post?
- How to overwrite ‘read more” text for artmag theme
- Call ACF data from functions.php [closed]
- Custom shortcode not being included in content paragraph [duplicate]
- Display post_meta-by_key on product catalogue
- trouble with passing class method data to outside function
- How to assign a div class to a echo function [closed]
- How to display milliseconds instead of seconds using timer_stop function? [closed]
- What are the parameters are used in add_action and filters?
- How can i hide content if not friend in Buddypress? [closed]
- Image width issue in IE [closed]
- new to javascript – using in instead of functions.php, not loading correctly
- How can I add authors to my blog pages?
- Filter to strip unnecessary attributes
- Integrating custom API for post content into Admin interface & Public Website [closed]
- Will my WordPress site become vulnerable after adding this functions which allows more HTML tags for subscribers?
- How to save custom made object in an array in a post meta field
- Get term count on a category page
- 500 error after upgrading from 5.0 to 5.4 and adding empty functions.php
- I want to add the alt attribute to all the photos!
- How do I add schema markup to individual WordPress posts without using plugins?
- syntax error, unexpected ‘$id’ (T_VARIABLE) – where is the issue though? [closed]
- how to handle multiple forloop?
- Add product to cart from functions.php
- Parse error: syntax error, unexpected ‘endforeach’ (T_ENDFOREACH) in [closed]
- Need to use an external variable inside a function
- Owl Carousel and WordPress Integration Via WP_Enqueue
- Woocommerce – Checkout error message
- Add custom css to theme
- Trying to get this function to show below the content
- Removing Facebook contact field from user contact not working
- add variable to actions/functions across different files (woocommerce)
- How do grab the main loop, with conditions, and output via shortcodes
- Why functions metaboxes is causing White Screen in Admin [closed]
- Rename file after title , one small problem
- Help finishing script to export WP user data when form submitted
- is_preview() always return false
- How do I add functionality to images?
- Adding and updating repeating custom field meta data
- How do I make my child theme’s CSS update when I save it?
- replacing words to “…” [closed]
- Display function from functions.php in tag.php
- AJAX values converted to PHP Variables?
- How can I customize the size of the header of my theme?
- reusing code in function and running it with loop
- Sessions in word press [duplicate]
- How to hook wp_mail to add a custom email as BCC for each sent email?
- Custom Post Type Search
- Explode Array from Repeatable Custom Field
- Directing to functions.php the correct way
- Retrieving next_post_link() and previous_post_link() in functions.php
- Replace & with &
- Decimal stripped from metabox number
- Which template file to edit to edit homepage in Mystile [closed]
- Getting full code of wp_list_comments instead of replacing it
- Does functions.php apply to every page?
- Make a PHP file with a function
- Getting error on function.php
- preg_replace specific Text to small latter strtolower [closed]
- Trying to decipher this code which is encoded with HTML Special Characters [closed]
- Remove Disqus JavaScript from homepage
- Which action is triggered before final output?
- Save output of the_content_rss into variable
- Changed functions.php file and now site is blank [closed]