Hot answers tagged

11

You can extend the get_the_archive_title filter which I've mentioned in this answer add_filter( 'get_the_archive_title', function ($title) { if ( is_category() ) { $title = single_cat_title( '', false ); } elseif ( is_tag() ) { $title = single_tag_title( '', false ); } elseif ( is_author() ) { ...


11

This is an alternative approach to @kaiser answer, that I found pretty fine (+1 from me) but requires additional work to be used with core WP functions and it's per-se low integrated with template hierarchy. The approach I want to share is based on a single class (it's a stripped-down version from something I'm working on) that takes care of render data for ...


9

Basic separated data structures To pass around data, you normally utilize a Model (that's the "M" in "MVC"). Let's look at a very simple interface for data. Interfaces are just used as "Recipes" for our building blocks: namespace WeCodeMore\Package\Models; interface ArgsInterface { public function getID(); public function getLabel(); } Above is ...


9

At the risk of getting voted down by everybody else here who thinks this is OK. I say: No, you shouldn't define functions in template files. This should be considered bad practice. Let's have a look at the documentation: Functions File Explained. The functions file behaves like a WordPress Plugin, adding features and functionality to a WordPress ...


9

In addition to the answer by @Welcher: There are some good "graveyard" examples in the core, where "functions come to die". You could use them as guidelines, e.g. regarding the documentation. Here's one such example for the permalink_link() under the wp-includes/deprecated.php /** * Print the permalink of the current post in the loop. * * @since 0.71 ...


8

PHP has a function for this purpose: exif_read_data I used this image for testing. Try this code for your purpose: add_action( 'add_attachment', 'create_post_from_image' ); function create_post_from_image( $id ) { if ( wp_attachment_is_image( $id ) ) { $image = get_post( $id ); // Get image height/width for auto inserting the image ...


7

Deprecation does not always equal being removed, it usually means that the item is marked for EVENTUAL removal from the API. Is this a method that will be called externally - as in by other plugins or developers? If this method is only ever used by the plugin internally, you can probably safely remove replace it with a better name function. If not, I'd ...


6

The accepted answer works to remove the Category: prefix from category archive titles, but not other taxonomy or post types. To exclude other prefixes, there are two options: Rebuild the title for all the variants used in the original get_the_archive_title() function: // Return an alternate title, without prefix, for every type used in the ...


6

You can use get_the_title() to return the current post title in the loop.


6

Answer has been updated because don't need to write function, only use wp_destroy_all_other_sessions function with initlike this add_action('init', 'wp_destroy_all_other_sessions'); Developed its plugin and publish on WordPress Download here :One Active session


5

Yes it is. It might get a bit clearer if you re-format this line $title .= " $sep " . sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) ); to: $title .= " $sep "; $title .= sprintf( __( 'Page %s', '_s' ), max( $paged, $page ) ); The first parameter of sprintf expects a format which is here the return value of the __ function. The second ...


5

You can set the expire date as post meta value. In single.php you can easily show that date by querying the post meta. See update_post_meta() and get_post_meta() Now, the second part of your question is pretty tricky. I can think of 2 solutions. Cron You can use wordpress cron to run at a time interval to check which post has been expired and ...


5

You can hook onto pre_insert_term, check the taxonomy and whether or not the user has the specified role as follows: function disallow_insert_term($term, $taxonomy) { $user = wp_get_current_user(); if ( $taxonomy === 'post_tag' && in_array('somerole', $user->roles) ) { return new WP_Error( 'disallow_insert_term', ...


5

Simple answer, don't pass variables anywhere as it stinks of using global variables which is evil. From your example it seems like you are trying to do an early optimization, yet another evil ;) Use the wordpress API to get data which is stored in the DB and don't try to outsmart and optimize its usage as the API do more then just retrieving values and it ...


5

Via using the dynamic_sidebar() you can change/edit the widgets in the widgets area of the appearance settings. If you use the_widget() you hard code it straight to template so you need to modify the code to change what is displayed. Both have their uses. Too many sidebars tend to clutter the widgets area, but is very good for users that don't want to mess ...


5

This is more of a general srcset and browser question, not a WordPress specific question. However, in general, you can't predict what browsers will do with the srcset information. For your specific example, it appears that you failed to clear the cache completely between your reloads. Note the 304 response indicating that your browser already has the image ...


5

You are returning inside your loop - so it returns on the first iteration, giving you one result only. You should build a string inside your loop instead, and only return when the loop is over. Something like $featured_properties = ''; if( $featured_query->have_posts() ): while( $featured_query->have_posts() ) : ...


4

I guess you mean add_action( 'pre_get_posts', 'private_groups' ); add_action( 'pre_get_posts', 'search_results' ); add_action( 'pre_get_posts', 'name_profiles' ); versus add_action( 'pre_get_posts', 'combined_into_single_callback' ); You can just check the difference using e.g. memory_get_usage() and timer_stop(). Many good plugins out there to help ...


4

I have reproduced your environment using the same theme and the same plugin. Out of the box, the plugin does not register the CPT with excerpt. You need to register it: add_post_type_support('advert', array('excerpt')); This should go in functions.php of your (child?) theme. My only question is: how and why were you getting excerpts on singular pages if ...


4

There are a few issues with your code as far as I can see that might or might not have an effect on your output Before I start, a few important point All code is untested and needs PHP 5.4+. All code should be tested on a local install first with debug set to true What I have given you is the bare minimum. Some pieces of code can be improved, and all can ...


4

I would highly recommend Daniel's much simpler solution over the one currently selected as correct: $user = get_userdata( $user_id ); if ( $user === false ) { //user id does not exist } else { //user id exists }


4

I doubt it would justify the effort of writing custom SQL queries for only 2500 posts. Within the wp_set_object_terms( $object_id, ... ) function we have: $object_id = (int) $object_id; so it's correct that it only takes a single post id as input. So you would need to loop over your $post_ids array but it might be sufficient to use the ...


4

Change this line setcookie( 'cookiename', 'cookievalue', time() + 3600, 'mysite.net/sites/site1/', 'mysite.net/sites/site1/'); to setcookie( 'cookiename', cookievalue, time() + 3600, COOKIEPATH, COOKIE_DOMAIN);` COOKIEPATH and COOKIE_DOMAIN are WP constants now, but the codex is not updated yet.


4

You need to enqueue the scripts as well, not only register them. You can, however, just simply enqueue a script without registering it if you are not going to enqueue it conditionally. I would try something like this: (Untested and requires PHP5.4+) add_action( 'wp_enqueue_scripts', enqueue_scripts, 11 ); function enqueue_scripts() { /** * Build ...


4

This should work: add_action( 'woocommerce_init', 'remove_wcpgsk_email_order_table' ); function remove_wcpgsk_email_order_table() { global $wcpgsk; remove_action( 'woocommerce_email_after_order_table', array( $wcpgsk, 'wcpgsk_email_after_order_table' ) ); }


4

get_currentuserinfo() is a pluggable function, it is not available during plugins load stage. That aside you shouldn't be adding filter conditionally, but use data provided by the filter. If you take a look at filter calls: apply_filters( 'auth_cookie_expiration', 14 * DAY_IN_SECONDS, $user_id, $remember ) $user_id is provided as second argument. You ...


3

get_the_utm_vars doesn't work like you think it does. A function cannot return more than one value. Right now, when the first return is reached, that value is returned and none of the following lines are ever reached. You could have get_the_utm_vars return an array of values instead: function get_the_utm_vars(){ $utm = array(); $utm['source'] ...


3

Before we go into details, it should be noted that the guides you are referring to is only what it is, a guide. Just like coding standards (which not even the core developers stick to :-)), the Wordpress Handbook is just a guide to developers, it is not the alpha and omega. The basic idea is to use this guide and implement these guideline in such a manner ...


3

Another option is: <?php echo str_replace('Brand: ','',get_the_archive_title()); ?> Replace Brand: with whatever text you are wanting to get rid of. Its worth looking into the difference between get_the_archive_title() and the_archive_title() the_archive_title() returns an array get_the_archive_title() returns a string


3

using wp_die(); at the end of AJAX function fixed the issue for me. e.g add_action( 'wp_ajax_my_ajax_function', 'my_ajax_function' ); function my_ajax_function(){ echo json_encode($myvar); wp_die(); }



Only top voted, non community-wiki answers of a minimum length are eligible