How To Modify WordPress Without Changing The Core Coding

January 26, 2023
WooCommerce vs Shopify

Don't change the core coding!

WordPress itself and its plugins often require new feature updates and security updates. Those updates overwrite all the previous coding and that means any previous changes you may have made to WordPress and plugin coding are lost during the update. But luckily, WordPress and many plugins are written in such a way that programming changes are independent from the core coding and don't get lost.

Keep your code separate from WordPress core code

WordPress uses what are called "hooks". Hooks allow you to create completely separate coding in order to modify the WordPress core code at specific, pre-defined spots. And just as plugins and themes use hooks to modify the WordPress core, you can do the same with your custom code.

There are two types of hook - "action hooks" and "filter hooks".  Action hooks "allow you to add data or change how WordPress operates".  Filter hooks "give you the ability to change data during the execution of WordPress Core, plugins, and themes". See this link for more information, https://developer.wordpress.org/plugins/hooks/

WordPress has hundreds of action and filter hooks.  See this page for a list of actions, https://developer.wordpress.org/apis/hooks/action-reference/ and this page for a list of actions https://developer.wordpress.org/apis/hooks/filter-reference/

Your separate coding can be placed in the functions.php file of a child theme or placed in a new plugin.

Example of an action hook:


/** 
* Add text to the admin header 
*/
function 1dd_admin_styles() {
  echo "My custom text";
}
add_action('admin_head', '1dd_admin_styles'); 
  

This action uses a WordPress predefined hook called 'admin_head' . That action hook allows you to add or modify content of the WordPress admin header: My custom text

This code 'add_action('admin_head', '1dd_admin_styles')' tells WordPress to use the function called '1dd_admin_styles()' when WordPress displays the admin head.

Example of an filter hook:


 /**
 * Turn off comments for attachments, mainly images
 */
function 1dd_filter_comment_status( $open, $post_id ) {
    $post = get_post( $post_id );
    if( $post->post_type == 'attachment' ) {
        return false;
    }
    return $open;
}
add_filter( 'comments_open', '1dd_filter_comment_status', 10 , 2 );
  

This filter uses a WordPress predefined hook called 'comments_open'. That hook allows you to filter whether the current post is open for comments. In this case, the comments are turned off, i.e., return false;.

This code 'add_filter( 'comments_open', '1dd_filter_comment_status', 10 , 2 );' tells WordPress to use the function called '1dd_filter_comment_status' when WordPress displays the attachment/image.

For more information or help call 530-392-8798 or click here.