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

Edit 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 More

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

Update 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 More