Hot answers tagged

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

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 ...


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 ...


7

The sweet and short is to not break your head over naming conventions or consistency in WordPress, as these are non existant "standards". To be really honest, WordPress does not follow any fixed set of rules or standards, even though there is a handbook on coding standard available for WordPress developers. Yes, it would have being nice if they would have ...


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


6

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 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

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

customize should work. I was able to remove the Customize link with the following code: add_action( 'wp_before_admin_bar_render', 'wpse200296_before_admin_bar_render' ); function wpse200296_before_admin_bar_render() { global $wp_admin_bar; $wp_admin_bar->remove_menu('customize'); }


5

You can create new schedule times via cron_schedules: function my_cron_schedules($schedules){ if(!isset($schedules["5min"])){ $schedules["5min"] = array( 'interval' => 5*60, 'display' => __('Once every 5 minutes')); } if(!isset($schedules["30min"])){ $schedules["30min"] = array( ...


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

Simply use CSS p:empty { display: none; }


4

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(); }


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

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

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 ...


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 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

Remove the = sign after your echo statement... echo = "<p>Text Goes Here</p>"; ...becomes echo "<p>Text Goes Here</p>"; Final add_action( 'woocommerce_review_order_after_shipping', 'action_woocommerce_review_order_after_shipping'); function action_woocommerce_review_order_after_shipping( ) { echo "<p>Text Goes ...


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

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

Since WordPress 3.6+ There is a function to print out all the media in the content. get_media_embedded_in_content();


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

Or this more compact one will do the same : add_action( 'admin_bar_menu', 'remove_some_nodes_from_admin_top_bar_menu', 999 ); function remove_some_nodes_from_admin_top_bar_menu( $wp_admin_bar ) { $wp_admin_bar->remove_menu( 'customize' ); } Regards


3

These queries are in the wp_enqueue_media() function: $has_audio = $wpdb->get_var( " SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'audio%' LIMIT 1 " ); $has_video = $wpdb->get_var( " SELECT ID FROM $wpdb->posts ...



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