Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating Own Custom Hooks in WordPress

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 327
    Comment on it

    Hello readers, today I guide you "How to create own custom hooks in WordPress".

     

    A crucial but often avoided practice is adding custom hooks to your plugin so that other developers can extend and modify it, without having to fork it.

     

    Alike Core's hooks are created and called in the same way Custom made hooks are also created and called, with add_action() as well as do_action() and add_filter() as well as apply_filters().

     

    Examples:- Creating an Extensible Configurations Form #Creating an Extensible Settings Form

     

     

    This example shows a custom action being called at the conclusion of the form:

     

    <?php
    	function my_hook_function() {
    ?>
        Foo: <input id="first" name="first" type="text" />
        Bar: <input id="second" name="second" type="text" />
     
    <?php
        do_action( 'hook_form_settings' );
    }
    
    function add_hook_form_settings() {
        ?>
     
        New 1: <input id="third" name="third" type="text" />
        New 2: <input id="forth" name="forth" type="text" />
     
        <?php
    }
    add_action( 'hook_form_settings', 'add_hook_form_settings' );

     

    Creating Custom Post Type

    From this example, when the new post type is register , the parameters that determine it are passed through a filter, so another plugin can adjust them before the post type is created.

     

    function create_custom_post_type() {
        $custom_post_type_params = array( /* ... */ );
     
        register_post_type(
            'post_type_slug',
            apply_filters( 'create_custom_custom_post_type_params', $custom_post_type_params ),
        );
    }

     

    If you are want to modify any post type and then register it for that you need a call back function as in above code my call back function is create_custom_post_type. In this circumstance, the callback will change the post type from a set type to a hierarchical one.

     

    function change_custom_custom_post_type_params( $custom_post_type_params ) {
        $custom_post_type_params['hierarchical'] = true;
        return $custom_post_type_params;
    }
    add_filter( 'create_custom_custom_post_type_params', 'change_custom_custom_post_type_params' );

     

    That's it. I hope it helps you.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: