Hot answers tagged

179

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


142

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


84

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


68

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, ]);


60

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


58

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


48

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

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


39

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


32

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


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

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


28

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


27

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


26

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.


25

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


25

Special thanks to user42826. According to the codex: If WordPress is hosted behind a reverse proxy that provides SSL, but is hosted itself without SSL, these options will initially send any requests into an infinite redirect loop. To avoid this, you may configure WordPress to recognize the HTTP_X_FORWARDED_PROTO header (assuming you have properly ...


24

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


24

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.


23

Your edit got me to this idea, there indeed is no function called »woocommerce«, there is a class »WooCommerce« though. One thing to be aware of is, that the check has to late enough, so that plug-ins are actually initialized, otherwise - obviously - the class won't exists and the check returns false. So your check should look like this: if ( class_exists( '...


22

I've been using this .. function get_id_by_slug($page_slug) { $page = get_page_by_path($page_slug); if ($page) { return $page->ID; } else { return null; } } Hope this will help someone.


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

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

Okay so I figured this one out. function custom_dequeue() { wp_dequeue_style('et-gf-open-sans'); wp_deregister_style('et-gf-open-sans'); } add_action( 'wp_enqueue_scripts', 'custom_dequeue', 9999 ); add_action( 'wp_head', 'custom_dequeue', 9999 ); @milo was right. The plugin was re-enqueueing it so the deregistering it got that file to stop ...


20

jQuery Migrate is nothing but a dependency of the jQuery script in WordPress, so one can simply remove that dependency. The code for that is pretty straightforward: function dequeue_jquery_migrate( $scripts ) { if ( ! is_admin() && ! empty( $scripts->registered['jquery'] ) ) { $scripts->registered['jquery']->deps = array_diff( ...


19

In wp-admin/includes/update.php file if ( current_user_can('update_core') ) $msg = sprintf( __('An automated WordPress update has failed to complete - <a href="%s">please attempt the update again now</a>.'), 'update-core.php' ); else $msg = __('An automated WordPress update has failed to complete! Please notify the site ...


19

Before going too far down this path I would suggest that you familiarize yourself with PHP name resolution rules. To answer your actual question - When you namespace functions, the fully qualified name of those functions includes the namespace. In your example, you have defined two functions: \myPlugin\add_activation_notice and \myPlugin\activation_notice....


18

Child themes are allowed to override templates, not simply arbitrary PHP files. In WordPress, a theme consists of a bunch of PHP files which are used as Templates. You can find a list of those files in the Template Hierarchy. Those specific template files can be overridden with new ones, but unless the parent theme has some special means for you to ...


17

You can do it with printf(). E.g. printf( __( 'We deleted %d spam messages.', 'my-text-domain' ), $count );


17

Actually, the automatic update is pushed from wp.org. The update process still runs on your site, but in the background via wp-cron. When a new minor update is released, the guys at WordPress start to roll out the update. The actual update process is started after your site checked wp.org for updates, an update is theoretically available, and your site is ...


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