Tips and Tricks
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 MoreShow 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 MoreBack 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 MoreEdit Thumbnail Cropping in Media Library
To change how WordPress crops an image to create the thumbnail: Select the image in Media Library Click the “Edit Image” button under the large image If you only want the change to apply to the thumbnail select “Apply changes to thumbnail” in “Thumbnail Settings” Click and drag the cursor over the image to highlight…
Read Morehtaccess Redirect and Remove Query String
To redirect menu.php?diet=vegan to /new-page-name whilst loosing the query string use this redirect in the .htaccess file RewriteRule ^menu\.php$ /new-page-name? [L,R=301] The trailing ? removes the query string
Read MoreChange 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”><<Previous</span>%title’, ‘twentyfourteen’ ) ); next_post_link( ‘%link’, __( ‘<span class=”meta-nav”>Next>></span>%title’, ‘twentyfourteen’…
Read MoreDisable all Plugins in the WordPress Database
Run this query on the wp-options table to disable all plugins: UPDATE wp_options SET option_value = ” WHERE option_name = ‘active_plugins’;
Read MoreUpdate all URLs in database except in serialized data fields
Updating URLs in the database can cause problems if you include those in serialized arrays. These queries leave serialized arrays untouched: UPDATE wp_options SET option_value = REPLACE(option_value, ‘http://devdomain.local‘, ‘http://livedomain.com‘) WHERE option_value NOT LIKE ‘%{%’; UPDATE wp_postmeta SET meta_value = REPLACE(meta_value, ‘http://devdomain.local‘, ‘http://livedomain.com‘) WHERE meta_value NOT LIKE ‘%{%’; UPDATE wp_posts SET post_content = REPLACE(post_content, ‘http://devdomain.local‘, ‘http://livedomain.com‘)…
Read MoreEnable 'Read More' link in posts outside the default blog page
Add the $more variable before the content in the loop: global $more; $more = 0; the_content(‘[Read more…]’);
Read MoreAccess to password protected directory giving 404 file not found error
If you have a directory protected by a password using HTTP Basic Authentication (set up in cpanel for instance) you will get 404 errors due to the way the WordPress permalinks works. The solution is to add this to the top of your .htaccess file in the site root: #prevent 404s when accessing password protected…
Read More