Hot answers tagged

152

get_permalink() is only really useful for single pages and posts, and only works inside the loop. The simplest way I've seen is this: global $wp; echo home_url( $wp->request ) $wp->request includes the path part of the URL, eg. /path/to/page and home_url() outputs the URL in Settings > General, but you can append a path to it, so we're appending the ...


138

COMPLETE GUIDE TO EXCERPTS I've recently answered a few questions regarding excerpts, so I'm going to give a detailed explanation covering as much as I can. PREFACE There seems to be a couple of questions arising from this answer on where the code should go, and the answer is, it is really up to you and how you see fit. There are are a couple of options ...


77

When a post is updated there are some hooks that are fired: 'pre_post_update' is an action fired just before the post is updated, the argument passed are 2: $post_ID and $data that is an array of all the other database colums of the post table 'transition_post_status' is an hook fired on update, and pass 3 arguments: $new_post_status, $old_post_status and $...


60

Use get_page_by_path($page_path): $page = get_page_by_path( 'about' ); echo get_the_title( $page ); This will return a regular post object. Documentation: https://developer.wordpress.org/reference/functions/get_page_by_path/ https://developer.wordpress.org/reference/functions/get_the_title/


59

You need to pass an additional argument to get_terms(). The default is to hide "empty" terms-- terms which are assigned to no posts. $terms = get_terms([ 'taxonomy' => $taxonomy, 'hide_empty' => false, ]);


59

The method below is now out of date as WooCommerce have added the wc_product_meta_lookup table which also needs to be updated with some of the meta values. Woo have now provided a CRUD interface so use that instead. $post_id = wp_insert_post( array( 'post_title' => $title, 'post_type' => 'product', ...


54

If by 'blog page' you meant a static page set as posts page in the Reading Settings, then you could check it by doing this: if ( is_front_page() && is_home() ) { // Default homepage } elseif ( is_front_page() ) { // static homepage } elseif ( is_home() ) { // blog page } else { //everyting else } When you use is_home() and is_front_page(),...


45

A prettier solution would be to disable the capability of creating a custom_post_type: Simply pass the the parameter 'create_posts' => 'do_not_allow', in the capabilities array when calling register_post_type. $args = array( 'label' => __( 'Custom Post Type', 'text_domain' ), 'description' => __( 'Custom Post ...


39

While I strongly advise against this, and it will not speed things up, your usage is incorrect. WordPress already caches these things in the object cache/memory so it doesn't have to fetch it multiple times in the same request, you don't need to store the result and reuse, WP does that already out of the box. It's very likely your code is running slower as a ...


37

On a category page, you can use the function single_cat_title(), or the more generic single_term_title(). These functions pull from the global $wp_query object, via get_queried_object().


35

If you are merely trying to get a count, $wpdb->get_var(); along with using COUNT() in your sql will be better: ### Search for IP in database function postviews_get_ip($id, $ip) { global $post, $wpdb; $rowcount = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->wp_postviews_ips WHERE postid = $id AND ip = '$ip'"); return $rowcount; } ...


33

I know I'm three months late, but the function you want here is WordPress' get_date_from_gmt(). The function accepts a GMT/UTC date in Y-m-d H:i:s format as the first parameter, and your desired date format as the second parameter. It'll convert your date to the local timezone as set on the Settings screen. Example usage: echo get_date_from_gmt( date( 'Y-...


32

Just change the post_type bit to: 'post_type' => array('testimonial', 'other_post_type', 'another-post-type'), Assuming that taxonomy is valid across all 3 post types. Otherwise you'll have to leave that out. Why? You can pass an array to post_type field.


31

You could check to see if the 'WooCommerce' class exists, then run the code that requires WooCommerce. <?php if ( class_exists( 'WooCommerce' ) ) { // code that requires WooCommerce } else { // you don't appear to have WooCommerce activated } ?>


31

The error message shows that the class description_walker extends the Walker_Nav_Menu class and overrides the start_el() method, but the signature differs from the method definition in the parent class. As mentioned in comments, it would be best to contact the developer of your theme and ask for a corrected version of the theme. If the developer cannot be ...


30

To disable user email notification, add this in a plugin or theme: add_filter( 'send_password_change_email', '__return_false' ); FYI wp_password_change_notification() controls admin email notification when a user changes their password


29

Something I forgot in my previous comment was that shortcodes return content, both the suggested include and my alternative get_template_part will directly output the content (which is what you are seeing with the content appearing at the top of your page instead of where the shortcode is called). To counteract this we must use output buffering. Define the ...


24

First, on function widget: function widget( $args, $instance ) { extract( $args ); // Add this line $your_checkbox_var = $instance[ 'your_checkbox_var' ] ? 'true' : 'false'; // Change 'your_checkbox_var' for your custom ID // ... } On function update: function update( $new_instance, $old_instance ) { $instance = $old_instance; /...


24

Late answer, but in Wordpress 4.7 two new functions were introduced to address this question. get_theme_file_path() (for absolute file paths) and get_theme_file_uri() (for URLs) work just like get_template_part() in that they will automatically look in the child theme for that file first, then fallback to the parent theme. In your example, you could ...


24

To disable Admin email notification when a user resets their own password, create a Plugin (or Must Use Plugin) using the following code snippet: /** * Disable Admin Notification of User Password Change * * @see pluggable.php */ if ( ! function_exists( 'wp_password_change_notification' ) ) { function wp_password_change_notification( $user ) { ...


23

The unfortunate situation is that there are indeed two different options: Newer timezone_string, which saves PHP–style time zone. Older gmt_offset, which saves numeric float offset in hours. But in newer environments timezone_string actually overrides gmt_offset, the value returned by the latter will be based on the former. However the opposite isn't true —...


23

While I don't like taking credit for someone else's answer, I also don't like leaving a question without answer when the answer does exist and it's already been given. I hope Milo won't mind if I copy the answer from his comment. The register_taxonomy calls should be hooked to init.


22

Don't use global variables, as simple as that. Why not to use globals Because the use of globals makes it harder to maintain the software in the long term. A global can be declared anywhere in the code, or nowhere at all, therefor there is no place in which you can instinctivly look at to find some comment about what the global is used for While reading ...


22

Ok I seem to have got it working. I added this in the functions file... //Reposition WooCommerce breadcrumb function woocommerce_remove_breadcrumb(){ remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20); } add_action( 'woocommerce_before_main_content', 'woocommerce_remove_breadcrumb' ); function ...


22

You can use the following in your themes functions.php file: function is_blog () { return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type(); } And then put this in the file you are checking: <?php if (is_blog()) { echo 'You are on a blog page'; } ?> You can use Hooks in ...


22

They are defined as follows: define( 'ABSPATH', dirname(dirname(__FILE__)) . '/' ); define( 'WPINC', 'wp-includes' ); dirname is a PHP function that returns the path of the parent directory, and wp-includes is pretty self explanatory. I would say ABSPATH is better because it's one of the first things WP loads and it also looks better:) But there is no ...


22

Yes, please avoid closing PHP tags at the end of the file, not only with PHP 7, but with PHP 5 as well. Reason is that if you close the tag, anything that is after the tag, even a blank line, will be sent to output and will make PHP to send headers as well preventing cookie to be set, redirect to work, feed to be valid, and so on. I guess that you ever ...


21

You may use the below code to get the whole current URL in WordPress: global $wp; $current_url = home_url(add_query_arg(array(), $wp->request)); This will show the full path, including query parameters.


20

Previous answer didn't worked for me (says that code is invalid, on wp login page), probably because answer is 1,5 yr old, and something is changed in WP code, so I have updated this code a bit (also from wp-login.php), here it is: function retrieve_password($user_login){ global $wpdb, $wp_hasher; $user_login = sanitize_text_field($user_login); ...


20

Updating the posts $all_posts = get_posts( 'posts_per_page' => -1, 'post_type' => 'post' ); foreach ( $all_posts as $single ) { wp_update_post( array( 'ID' => $single->ID, 'post_title' => to_title_case( $single->post_title ) // see function below )); } Converting a string to "Title Case" And, while not ...


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