Register a script to be enqueued both in admin and front end

You can register the scripts earlier, for example on wp_loaded: add_action( ‘wp_loaded’, ‘register_all_scripts’ ); function register_all_scripts() { wp_register_script(…); } And then you enqueue the scripts whenever you need them: add_action( ‘wp_enqueue_scripts’, ‘enqueue_front_scripts’ ); add_action( ‘admin_enqueue_scripts’, ‘enqueue_back_scripts’ ); Use the same handles and names to avoid collisions with other scripts.

Why do some core functions get wp_ while others do not? What’s the rule?

This makes not that impressive answer, but – none. get_ is suffix usually means function returns something, the_ that function echoes something. wp_ doesn’t carry technical meaning and inconsistency in naming is from many developers participating and lack of movement to unify (moving forward is considered more important than major cleanups of older stuff to … Read more

Remove custom post type slug from URL

That’s how you can do first part of the job – get rid o CPT slug in post link (eg. news post type). function df_custom_post_type_link( $post_link, $id = 0 ) { $post = get_post($id); if ( is_wp_error($post) || ‘news’ != $post->post_type || empty($post->post_name) ) return $post_link; return home_url(user_trailingslashit( “$post->post_name” )); } add_filter( ‘post_type_link’, ‘df_custom_post_type_link’ , … Read more

How to end the excerpt with a sentence rather than a word?

This requires PHP 5.3+ (WP requires PHP 5.2.4+) add_filter(‘get_the_excerpt’, ‘end_with_sentence’); function end_with_sentence($excerpt) { $allowed_end = array(‘.’, ‘!’, ‘?’, ‘…’); $exc = explode( ‘ ‘, $excerpt ); $found = false; $last=””; while ( ! $found && ! empty($exc) ) { $last = array_pop($exc); $end = strrev( $last ); $found = in_array( $end{0}, $allowed_end ); } return … Read more

Get default permalink structure from pretty URL’s

First of all I have to say that wp_get_referer() is not 100% reliable because it relies on $_SERVER[‘HTTP_REFERER’] that is not 100% reliable, from php.net docs: The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set … Read more

How does nonce verification work?

TL;DR In short, wp_verify_nonce() uses that value because it expects that value as its first argument. wp_verify_nonce() arguments wp_verify_nonce() receives 2 arguments: $nonce $action The value in the hidden field (‘cabfd9e42d’ in your example) represent the $nonce. 1st argument is the nonce, and comes from the request In fact, wp_verify_nonce() have to be used like … Read more

Customize Edit Post screen for Custom Post Types?

Some of these questions are answered here: Set Default Admin Screen options & Metabox Order To remove the permalink metabox: function my_remove_meta_boxes() { remove_meta_box(‘slugdiv’, ‘my-post-type’, ‘core’); } add_action( ‘admin_menu’, ‘my_remove_meta_boxes’ ); additionaly, you will have to hide #edit-slug-box with css or javascript. see: Loading External Scripts in Admin but ONLY for a Specific Post Type? … Read more