You can use a static variable to achieve this:
add_filter( 'the_content', 'asdf_the_content', 99, 1 );
function asdf_the_content( $content ) {
static $has_run = false;
if ( $has_run ) {
return $content;
}
$has_run = true;
// check if the_content has already been
// filtered by some other function
$content = ucwords( $content );
return $content;
}
The $has_run
variable will be false
on the first run, and subsequent runs it will be true
and the code will not continue. Static variables like this inside a function maintain their values during each execution, instead of initializing like normal variables.
Another example:
function add_one() {
static $total = 0;
$total++;
echo $total;
}
add_one(); // 1
add_one(); // 2
add_one(); // 3
Related Posts:
- Add custom options to the wplink dialog
- How to show page content in feed?
- Filter specific shortcode output?
- Insert new element to array with add_filter
- remove_filter( ‘the_content’, ‘wpautop’ ); only for certain post types
- How can I extend the Gutenberg table block transform to allow colspans/rowspans on pasted table elements?
- Filter Widget Title Wrap
- Customizing WordPress the_title with add_filter
- How can I send data to admin-ajax via JS Fetch?
- Disable resizing of gif when uploaded
- How to change Woocommerce breadcrumbs content?
- How can I display image metadata?
- I’m using a filter to remove the tags auto wrap, but there still wrapped elements?
- How to prepend text to the_content, but after img/shortcode
- How to enable visual editor when editing comments on the dashboard?
- Can’t get wp_title filter working in twenty sixteen child theme
- add_action uses ‘echo’ add_filter uses ‘return’, why?
- Cron schedule interval through plugin options?
- wpmu_signup_user_notification filter not working
- How to check a filter are applied
- How do I add a listbox to the TinyMCE editor?
- How to remove google font in WordPress for only single page?
- Remove tags from the kses filter
- Overwrite default WordPress wording
- Why anything done on comments_array hook gets reset?
- Using ‘posts_where’ on a query with a custom field
- Wrap h1-h6 in a div
- add filter login_redirect does not contain original requested redirect
- get_header and hook avoid normal call
- How to add numeric slug for child page in WordPress 5.9?
- Dropdown filter in custom posts
- Custom Login Errors and variables I can use
- How to order by taxonomy using wp_query
- ‘the_content’ filter removing html tags from post content
- Feed, RSS not able to clear the cache and cannot change the limit of cache life time
- WP action/filter to modify title before header output and article output?
- Custom view counts not updating correctly
- WP Filter to change the URL of an Anchor wrapping the last gallery image
- Gutenberg – Add align controls to a custom block
- To add a custom filter based on a meta field
- I can not include page to wordpress function add_filter, the_content
- How to create Woocommerce Product Filter Widget [closed]
- Add default user field to WooCommerce checkout [closed]
- Filter to shorten column content on edit-tags.php
- Filtering Comment Reply Links (comment_reply_link_args) for “infinite replies” in nested comments
- Using a filter to modify Genesis wp_nav_menu
- Upload restrictions – upload_mimes – filter: Adding multiple MIMEs for a single extension and adding multiple extensions for a single MIME type?
- Filters on Login Page
- the_excerpt filter doesn’t work as expected
- How to filter link?
- Conditional does not work with add_filter
- What is the earliest hook to modify post content?
- How to modify only part of a function through the filter
- How can I apply filters in my class that extends Walker_Nav_Menu?
- Pass debug_backtrace() in WordPress filter
- How should I be using filters and is_single together?
- How to edit embed filter for youtube video to allow responsive full width layout
- Remove get_template_part() from custom theme
- How to access page variable inside action hook
- Shorten the title length
- Problem with Class, Filters and Callbacks
- Renaming wordpress login and get new password button
- Hook in a sidebar widget and add some markup
- preg_match() not working with post content
- Search a title word through query_posts (not the exact match)
- Parse a shortcode differently based on on what it’s nested in
- Elementor Image Hover + Filter Grid [closed]
- How to redirect template_directory to subdomain relativ url?
- ‘manage_users_custom_column’ is a filter, but ‘manage_posts_custom_column’ is an action. Why?
- comment_notification_text filter not working
- Line Breaks are stripped off when direction property is found
- Scanning for custom embed and prefetching
- WordPress remove_filter not working
- How to exclude or include categories in wp rest API without query parameters?
- Remove and replace woocommerce add to cart button [closed]
- Hook to change the site URL
- Replace a specific URL on all apperances on the Website (Maybe a filter?)
- How to add lazy field in content endpoint using Gutenberg blocks
- Strange behaviour of REGEX in a WordPress filter (trying to suppress emtpy paragraphs)
- Modify Contextual Help
- how to use apply filter for Class?
- WordPress set featured image to first image of the post
- Modify WordPress search behaviour in backend?
- Re-order search results with posts_orderby filter and post meta value
- ‘the_content’ Filter delivers empty string with lengh (608)
- Testing requested query in pre_get_posts
- Output dynamic_sidebar_params in wp_head
- How to sort posts according to meta value?
- Converting restricted html in comments to bbcode
- What is the proper/best way to have multiple add_filter for wp-job-manager-resume
- Modify WooCommerce email shipping text value
- Using Filters To Change Page Title
- using posts_where for meta data on pre_get_posts
- can’t output gray scaled image I’ve created using add_image_size
- remove_filter excerpt_more from a plugin class
- How properly write function to filter content in a template for plugin “multiple content blocks”
- How to elect position of new item output in a dropdown when using add_filter
- Query Multiple Filters, one with Meta
- How to remove a filter that is an object method?
- Filter a custom post type by custom field in admin