1 2 |
update wp_posts set post_content = replace(post_content,'Text to find','text to replace with'); |
Category: Code Snippets
How to Exclude pages from admin edit pages list
This is a great little snippet that will exclude pages based on the the ID from the admin pages list. Just add this snippet to the functions.php of your wordpress theme and update the array with the page ID’s you wish to hide. Please note this does not stop a post from being editable this snippet only hides the page from view.
1 2 3 4 5 6 7 8 9 |
add_action( 'pre_get_posts' ,'exclude_this_page' ); function exclude_this_page( $query ) { if( !is_admin() ) return $query; global $pagenow; if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) ) $query->set( 'post__not_in', array(23,28,30) ); // page id return $query; } |
How to Change the currency symbol in WooCommerce
In this case its Australian Dollars
1 2 3 4 5 6 7 |
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2); function change_existing_currency_symbol( $currency_symbol, $currency ) { switch( $currency ) { case 'AUD': $currency_symbol = 'AUD$'; break; } return $currency_symbol; } |
Create your own pluggin for your custom theme functions
- Firstly, navigate to your WordPress website via FTP
- From there, you should navigate to wp-content/plugins
- Create a new folder
- Create a PHP file with the same name within your plugin’s folder
- Insert the following code into your new PHP file
1 2 3 4 5 6 7 |
<!--?php /** * Plugin Name: Custom Functions Plugin * Description: This plugin contains all of my awesome custom functions. * Author: Your Name * Version: 0.1 */ /* Your code goes below here. */ /* Your code goes above here. */ ?--> |
How to remove width and height attributes images in posts
Looking to remove the width and height attributes from images in your posts? Is this because you are using a responsive wordpress theme. Just add this snippet to the functions.php of your wordpress theme and all of the width and height attributes will be removed from your images.
1 2 3 4 5 6 7 8 |
<?php add_filter( 'post_thumbnail_html', 'remove_wps_width_attribute', 10 ); add_filter( 'image_send_to_editor', 'remove_wps_width_attribute', 10 ); function remove_wps_width_attribute( $html ) { $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html ); return $html; } ?> |
How to display custom field value from next and previous post
Adding this snippet within the loop of your wordpress theme will display the custom field value from the previous and next posts. Just change the CUSTOM_FIELD on line 4 and 5 to the name of the custom field you wish to display.
1 2 3 4 5 6 7 8 9 10 |
<?php $previous_post = get_previous_post(); $next_post = get_next_post(); $prev_value = get_post_meta( $previous_post->ID, 'CUSTOM_FIELD', $single = true); $next_value = get_post_meta( $next_post->ID, 'CUSTOM_FIELD', $single = true); ?> <?php if ( $prev_value != '' ) : ?> <p><?php echo $prev_value; ?></p> <p><?php echo $next_value; ?></p> <?php endif; ?> |
Add category name to body class of post
Some times you mat need to style posts differently depending on which category they are in. WordPress by default does not add the category to the body class so you need to add this to your themes’ functions.php file or create a plugin for it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php function pn_body_class_add_categories( $classes ) { // Only proceed if we're on a single post page if ( !is_single() ) return $classes; // Get the categories that are assigned to this post $post_categories = get_the_category(); // Loop over each category in the $categories array foreach( $post_categories as $current_category ) { // Add the current category's slug to the $body_classes array $classes[] = 'category-' . $current_category->slug; } // Finally, return the $body_classes array return $classes; } add_filter( 'body_class', 'pn_body_class_add_categories' ); ?> |
Hire Me
If your interested in hiring me then please use the form below to send me details of your project and will be in touch shortly.