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:
- How to add a data attribute to a WordPress menu item
- Order posts (across the whole site) by metadata date
- Functions.php code that only runs on localhost?
- hook a functions to change wp-config from functions.php
- Relative URLs and hide /wp-content/themes/
- How to restrict actions and filters “properly” by conditions
- Extending auth_cookie_expiration based on user role
- Check if the user has explicitly set an excerpt
- Slugs as breadcrumbs for Pages
- Removing Unnecessary Text from Admin Menu without CSS
- Add inline css to theme
- Shortcode with custom content attribute?
- Add a Post Thumbnail to an RSS Feed with custom size
- Display WordPress Search
- Automatically add author’s name to post_tag
- Print Dashboard menu name and link
- Remove_filter (‘the_content’, ‘wpautop’) is not working
- Develop function get_template_part in functions.php
- Where does function_exists() look to decide whether a function exists? [closed]
- Change comment_reply_link URL
- Ajaxing function in widget class
- Clean-up script tags
- WordPress function for 1 to for many
- How can you limit srcset on a single type of page?
- Remove Font-Awesome MaxCDN Link & Load Locally
- How to fix Warning: call_user_func_array()?
- How to get specified parent page title in my function
- Select pages by category
- wp_register_script(… $in_footer = true) not working
- I want to get product attirbute in the section below
- Restore Image Title Text
- Output and filter data from a XML url
- Where to start learning more about wordpress templates [closed]
- What did I do wrong in my functions code, that will not change the “Get New Password” text to “Send It”?
- How do I create a function that simulate the click on Update button for all posts?
- Multisite Ajax serialize return error [object Object]
- Full page template function
- Redirect from “private” page and functions.php which tag add to add_action()?
- PHP mixed with some JS code to update WordPress theme settings
- Display gallery on bottom after content
- Wp admin – Set default value to 999 in comments
- WordPress listen to $_POST in functions.php
- WordPress Shortcode function display outside of widget
- Get rid of the widget Item Class=”widget-item”
- wordpress use single ajax in place of multiple ajax requests in a smarter way
- Add Element as a Filter to the_content
- Storing password (functions.php)
- Can’t properly set the_title add_filter to show short_URL
- WordPress function, Run using Crontab
- Check if the image size is available and if not use ‘full’ image size
- Infinite counting in WordPress
- Tags break custom Excerpt function
- Can set_post_thumbnail be used to remove a thumbnail?
- issue with if/elseif in_array inside foreach loop display only one post
- Specify multiple categories for custom post template – FATAL ERROR
- Fail to get the total number of posts
- What is the correct way to include my new functions and scripts in WordPress?
- Call two different function.js depending on page
- Display a random tag but using cron to control frequency of change
- Entirely Different Functions.php per Page?
- Problems with functions.php! Error! [closed]
- Output comment_author in array
- Problem with shortcode inside a shortcode
- Hide Author By-Line if After Certain Date
- sort title descending with title with number
- Add #primary at the end of navlink permalinks on single posts
- Is there anyway to force is_singular() to be true when not loading template?
- wp_title() return random number
- Postback redirect through add_action is not triggered
- How to add class dynamically from templates
- How to offset main query without affecting other queries and backend
- ACF Date Form in Custom Admin Field
- How to change form action of wp-login page with a function
- How to add link rel tags on paginated posts?
- How to pass data from a plugin to functions.php
- Function to disable “href” anchor tags that contain “a data-name” on mobile only
- Execute jQuery with custom event listener after successfully add an item to the cart
- WordPress hooks to call a function inside a construct
- WordPress undefined function error on using add_action() in functions.php
- How to make an If Else on Excerpt Filter
- Translation Function missing text-domain [closed]
- Retrieve a custom form field modified by a filter
- Custom taxonomy not refreshing page on add
- Pagination in Search result
- Custom field not updating via functions upon publishing
- disable WP_error: authentication_failed
- Load a Child Theme’s style.css just before the closing tag
- Can’t wrap my head around this function – any help would be appreciated
- Figure inlined width trigger distorted layout
- Exclude javascript in certain page url and all following url’s after forward slash
- Changing functions.php and .htaccess files
- Shortcode Variations?
- Code in functions.php appearing on front-end and dashboard [closed]
- How to test for a class (from a plugin) in functions.php
- Child Theme not working – CSS gone
- Custom shortcode not being included in content paragraph [duplicate]
- Sessions in word press [duplicate]
- Which template file to edit to edit homepage in Mystile [closed]
- “woocommerce_output_related_products” not working
- woocommerce_sort_product_tabs() expects an array