Code

Enable or Disable Automatic Updates of WordPress Core, Themes and Updates using Functions.php

Placing this in functions.php will enable automatic updates of the WordPress core, plugins and themes. /* configure automatic updates */ add_filter( ‘auto_update_core’, ‘__return_true’ ); add_filter( ‘auto_update_plugin’, ‘__return_true’ ); add_filter( ‘auto_update_theme’, ‘__return_true’ ); Changing __return_true to __return_false disables said updates. These can be further refined to limit the level of updates and include only certain plugins.…

Read More

Display Posts Shortcode – Change Date Format

Default date format in the Display Posts Shortcode plugin is hard coded in the plugin but it can be changed using a filter. By default the date format is (8/29/15) which is useless for countries other than the USA. The following function can be placed in the THEME/functions.php file to change the format so that…

Read More

Check for Featured Image and Display It With The Caption

First check for the existence of the image then display it in the content area: In the template us this: // check if the post has a Post Thumbnail assigned to it. if ( has_post_thumbnail() ) { the_post_thumbnail(); } This defaults to the Thumbnail size under Settings. Size can be set by adding sizing parameters…

Read More

Override WordPress Options using a Filter

The basic format for the add_filter function is: add_filter(‘pre_option_’.$option, ‘override_function_name’); Where $option is the name of the Option as found in the wp_options table of the database. This example uses a filter to update the admin_email set in Options to that of the current user, so ‘admin_email’ becomes ‘pre_option_admin_email’. add_filter(‘pre_option_admin_email’, ‘override_option_admin_email’); function override_option_admin_email() { global…

Read More

Determine User Role by Name in WordPress

This function returns the user role by name rather than by capabilities which is useful for determining whether the current user is a Wholesale customer for instance. It can also return the name of the user role given the user ID as a parameter. /** * Checks if a particular user has a role. *…

Read More

Restrict Non-WordPress Page Access to Logged in User

This is useful for non WordPress pages on a WordPress installation. Add this to the top of the page: <?php include(‘wp-load.php’); if(!current_user_can(‘editor’)) { wp_redirect( wp_login_url() ); } ?> To restrict access to admin only change “editor” to “administrator”. wp-load.php loads the WordPress functions so yo can use the current_user_can() function

Read More

Show content to logged in users only

This function can be used to make content visible only to logged in users: <?php if ( is_user_logged_in() ) { echo ‘Welcome, registered user!’; } else { echo ‘Welcome, visitor!’; } ?> Full details here: http://codex.wordpress.org/Function_Reference/is_user_logged_in

Read More

Back button using Javascript that works in Chrome

Creates a link to show the previous page which works in Chrome as well as other browsers. Place the function in the :  Then create a link to call the function and load the previous page: Previous Page  Without the “return false” in the function the back button doesn’t work in Chrome. Recently I have…

Read More

Change Single Page Navigation Text and Styling

In single.php replace: // Previous/next post navigation. twentyfourteen_post_nav();  with this: <nav class=”navigation post-navigation” role=”navigation”> <h1 class=”screen-reader-text”><?php _e( ‘Post navigation’, ‘twentyfourteen’ ); ?></h1> <div class=”nav-links”> <?php if ( is_attachment() ) : previous_post_link( ‘%link’, __( ‘<span class=”meta-nav”>Published In</span>%title’, ‘twentyfourteen’ ) ); else : previous_post_link( ‘%link’, __( ‘<span class=”meta-nav”>&lt;&lt;Previous</span>%title’, ‘twentyfourteen’ ) ); next_post_link( ‘%link’, __( ‘<span class=”meta-nav”>Next&gt;&gt;</span>%title’, ‘twentyfourteen’…

Read More

Get Link to Featured Image for use in View

In functions.php create function and shortcode /** *************************************************** * get image src for featured image of post (large version) */ /* Register our short code at the appropriate time during the WordPress Boot process. */ add_action( ‘init’, ‘wpk_register_shortcodes’); function wpk_register_shortcodes(){ add_shortcode(‘my_get_thumbsrc’, ‘my_get_thumbsrc’); } /* Function to return the source url for the featured image of…

Read More