Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

1

Thanks Lucas! I found this code as an example in several places ... still interesting that the && doesn't work ... but this does :) add_filter( 'term_link', 'slick_term_to_page', 10, 3 ); function slick_term_to_page( $url, $term, $taxonomy ) { if ( $term->term_id != 42 ) : // if ( $taxonomy != 'highlight' && $...


1

The second parameter here would be useful in targeting the appropriate context. add_filter( 'posts_where', 'Where_Events', 10, 2 ); function Where_Events( $where, $wp_query ) { } The global $wp_query would be able to tell you whether the query was for the main query by checking $wp_query->is_main_query().


0

I managed to solve this issue using pre_get_termsas an action and then updating the query_vars $current_user = get_current_user_id(); $user_field = "user_".$current_user; $user_categories = get_field('post_categories', $user_field); if(in_array('category',$terms->query_vars['taxonomy'])){ $terms->query_vars['include'] = $user_categories; } ...


1

Try the suggestion below: This assumes the <figure> element holding an image has a data-display-alignment attribute to match the alignment class applied attached that is somehow surfaced as a parameter in the wp_calculate_image_sizes hook: /** * Add custom image sizes attribute to enhance responsive image functionality * for content images. * * @...


2

Is it possible to bind a function to a filter hook via Ajax? No, because page requests are self contained. When you request something from PHP, everything gets loaded from a fresh clean slate. At the end of the request, that slate is discarded. This is different from say a Node application that is always running. So, if you make an AJAX request and ...


0

So I tried Pawan's answer and while it did work, it was applying the filter all over the place, not to the custom post types I wanted. So I made a slight modification that should make this work: public function filter_exhibitions($post_type, $which) { $target = 'exhibition'; // Created this target variable and removed the $_GET stuff if($post_type == $...


0

The way apply_filters() works is that the developer provides a name for the filter, and the value that can be filtered. This lets other code modify that value by adding a filter. In addition the value, however, apply_filters() can also pass additional values to hooked functions so that they can be used inside the filter callback. These are passed as ...


0

The tag template page began displaying posts for specific tags once I replaced the argument statement with the following, which is great!!: $tags = get_the_tags(); $tag = $tags[0]->name; $args = array( 'post_type' => 'post', 'post_status'=>'publish', 'category_name' => 'NEWS', 'tag_slug__and' => $tag, 'posts_per_page' =&...


0

You can create a custom shortcode wrapper that will execute the shortcode with the desired attributes, so you would use [jobs-custom] in the page: add_shortcode('jobs-custom', 'custom_jobs_shortcode'); function custom_jobs_shortcode($atts) { if (is_user_logged_in()) { $per_page = get_user_meta(get_current_user_id(), 'jobs-per-page', true); }...


0

I had the same problem of needing to only apply $post_clauses to specific loops/queries and was able to use THIS solution provided by Stephen Harris: You can access query variables (including custom ones) via the WP_Query::get() method. For example: $my_query = new WP_Query( array( ... 'wpse105219_custom_var' => 'foobar', ... )...


0

The views_edit-page filter returns an array of strings. By default this will be something like this: array ( 'all' => '<a href="edit.php?post_type=page" class="current" aria-current="page">All <span class="count">(1)</span></a>', 'publish' => '<a href="edit.php?post_status=publish&#038;post_type=page">Published &...


0

Perhaps you could push the variable contents to error log for later inspection? Like so, function addFilter($views) { // error_log is native php function to log stuff // print_r prints human-readable information about a variable, // print_r second parameter makes the function return result instead of echoing it error_log( print_r( $views, ...


0

Filter should modify the given value and return it. It should not print anything. But your foreach loop does exactly opposite - it prints its output and doesn’t append anything to result. So when you use it to append its result to the content, it prints its result and doesn’t return anything - so nothing gets appended. One way to fix it is to use buffering:...


0

Questions of the 'which is best' type are impossible to answer, because they depend on the eventual use case. If the users who are building the individual sites are familiar with object oriented programming the first approach may be cleanest. If they are less schooled in OOP, you might go for the filters. But I think you are overlooking the easiest approach,...


1

When customer returns from a gateway without paying, it falls on order received (thankyou) page with 2 possible status: failed or pending, and there is 2 possible action hooks: woocommerce_thankyou_{$order_payment_method} (composite hook with $order_id available argument) woocommerce_thankyou (with $order_id available argument) You can target that case ...


0

You can catch specific status change by using this action hook 'woocommerce_order_status_{status}'. It is defined in the WC_Order class. This is how you define it: /** * Executed when the status is changed to failed. * @param int $order_id * @param \WC_Order $order */ function wpdg_9291_woocommerce_order_status_failed( $order_id, $order ) { // Do ...


0

Thanks for the help. I changed the code a little to work with the current WordPress version. Also changed it to support more than one custom template. I bet there is a better way of doing this but it worked for me. /** * Load Template with Plugin */ function yourname_add_page_template ($templates) { $templates['page-one.php'] = 'title here One'; $...


1

I tested it myself with the code you use and it only works with Gutenburg disabled. It seems to be related to the new editor. Assuming you want to use the new editor, and depending on your theme and plugins this code may work for you (it worked from my tests): add_action( 'after_setup_theme', function(){ // this removes the feature image panel ...


1

@MikeNGarret's answer pointed me to the interesting function wp_get_mime_types(). Within this function you can look up how to correctly add MIME types to the upload_mimes filter (https://core.trac.wordpress.org/browser/tags/5.1.1/src/wp-includes/functions.php#L2707). The correct answer therefore is: // allow SVG and video mime types for upload function ...


1

This filter only accepts strings for mime types. It also already has support for webm, mp4, mpeg, ogv, and ogg. I think you can remove everything except the line for SVGs. A word of warning, WordPress doesn't support SVG upload by default because it's a security concern. Be careful about enabling this. At the least, use something like the Safe SVG plugin ...


0

This function based on @Digerkam answer. Added compare if $def['function'][0] is string and it's finally worked for me. Also using $wp_filter[$tag]->remove_filter() should make it more stable. function remove_class_action($tag, $class = '', $method, $priority = null) : bool { global $wp_filter; if (isset($wp_filter[$tag])) { $len = ...


0

You might need to add a plugin (or add your own comment filter) to 'count' the number of URLs in a comment before it is stored. The Akismet plugin is a good one for catching a lot of spam. For one the sites that I managed, I had a similar problem. I ended up writing a plugin to allow for counting URLs (and lots of other customization of the comment form). ...


0

Well the best way is to use a antispam plugin like Aksimet. Other captcha plugins can be helpful too, you just have to look for the captcha keyword in wordpress repository. If you want to do this manually, I can give you a hint. First of all, you need to know which action hook you want. I couldn't find an action that fires before the comment insertion, but ...


0

Taking @Welcher's suggestion of using pre_get_posts, I took my existing code and compiled the following to filter edit.php page for my custom post based on an ACF field granting the current user's access rights: add_action( 'pre_get_posts', 'customize_products_for_admins' ); function customize_products_for_admins( $wp_query ) { if ( is_admin() &&...


1

Your best bet is to use the pre_get_posts hook to adjust the query on the admin side. Your logic is pretty complicated so I won't try to give you a working example but the snippet below will get you started. Basically, you want to be sure your in the admin and managing the main query. Codex link add_action( 'pre_get_posts', 'customize_admin_query' ); ...


Top 50 recent answers are included