Tag Info

Hot answers tagged

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


8

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


7

If you look at the source code of get_the_archive_title(), you will see that there is a filter supplied, called get_the_archive_title, through which you can filter the output from the function. You can use the following to change the output on a category page add_filter( 'get_the_archive_title', function ( $title ) { if( is_category() ) { ...


7

If you need this to be a PHP solution. You could do something like this using 'the_content' filter. This will add the class name "content-img-wrap" to any paragraph that wraps only an image tag when Wordpress prints your post content. So this will not wrap a paragraph with an image and a span. See update if that is what your looking for. add_filter( ...


6

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() ) { ...


6

PHP has a function, get_included_files() that returns all the file have been includedduring a request. However, if you use that fucntion you obtain all the files required: WordPress core files, plugin files... You need a way to: filter out files that do not belong to theme and child theme (if any) include only files loaded after main template has been ...


6

As @cybmeta already pointed out, you are too early with your removal. So you have to defer the actual removal, for instance like so: add_action( 'after_setup_theme', 'wpdev_170663_remove_parent_theme_stuff', 0 ); function wpdev_170663_remove_parent_theme_stuff() { remove_action( 'after_setup_theme', 'et_pb_setup_theme' ); }


6

Try this: if ( has_excerpt( $some_post_id ) ) { // Do something here? } else { // Or here? } References: has_excerpt


6

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


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

I am not sure why your query is returning more IDs than necessary. The $args for get_users look correct. By default get_users does not support orderby=rand, but you can overwrite that option. See below: function random_user_query( &$query ) { $query->query_orderby = "ORDER BY RAND()"; } // Usage: [random_users how_many = 3] add_shortcode( ...


5

All you can do is bump the priority of your hook: add_action( 'wp_head', 'dns_prefetch', 0 /* Highest priority */ ); but does this put my code just after the start of tag? No, but it will output before any other function attached to wp_head i.e. other <link />'s, most stylesheets, plugin scripts etc. To put it right after <head> you'll ...


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


4

You can make use of the conditional tag is_page_template() This Conditional Tag allows you to determine if you are in any page template. Optionally checks if a specific Page Template is being used in a Page. This is a boolean function, meaning it returns either TRUE or FALSE. You can try something like if( is_page_template( 'template-flat.php' ) ) { ...


4

Your problem isn't that you did the wrong thing, it's that you did it at the wrong time. When it happens is important, not where. In a child theme, the functions.php is loaded, then the parents, so you're doing this: Remove the first_paragraph function from the_content filter Add the first_paragraph function to the_content filter Clearly this is the ...


4

Add the following code to your functions.php: function allow_contributor_uploads() { $contributor = get_role('contributor'); $contributor->add_cap('upload_files'); } if ( current_user_can('contributor') && !current_user_can('upload_files') ) { add_action('admin_init', 'allow_contributor_uploads'); } This will add the upload_files ...


4

finely made its answer function wp_destroy_all_other_sessions() { $token = wp_get_session_token(); if ( $token ) { $manager = WP_Session_Tokens::get_instance( get_current_user_id() ); $manager->destroy_others( $token ); } } add_action('init', 'wp_destroy_all_other_sessions'); You can use it on any plugin or direct you can ...


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

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


4

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


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

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.


3

What about using the WordPress function get_ancestors(), which Returns an array containing the parents of the given object. To be exact an Array of ancestors from lowest to highest in the hierarchy We can easily create a function to return a list by using implode to do so: function wpse185971_get_ancestors_list( $object_id, ...


3

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


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


3

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



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