简体   繁体   中英

remove code with add_action in wordpress?

Hi Im still getting used to using actions in wordpress. Im just wondering is what I want possible here.

I want to remove the echo s that are generating some of the form below without touching the code. I see there is a do_action before and after them. But Im wondering are they of any use to me for doing what I want, and if so how do go about affecting the content in between the do_action s.

For example, just say I wanted to remove

            echo "
        <p>
            <label for='log'>".__( 'Username', 'jigoshop' )."</label>
            <input type='text' name='log' id='log' class='input-text username' />
        </p>
        ";

from...

do_action( 'jigoshop_widget_login_before_form' );

        // Get redirect URI
        $redirect_to = apply_filters( 'jigoshop_widget_login_redirect', get_permalink( jigoshop_get_page_id('myaccount') ) );
        $user_login = isset( $user_login ) ? $user_login : null;

        echo "<form action='".esc_url(wp_login_url( $redirect_to ))."' method='post' class='jigoshop_login_widget'>";

        // Username
        echo "
        <p>
            <label for='log'>".__( 'Username', 'jigoshop' )."</label>
            <input type='text' name='log' id='log' class='input-text username' />
        </p>
        ";

        // Password
        echo "
        <p>
            <label for='pwd'>".__( 'Password', 'jigoshop' )."</label>
            <input type='password' name='pwd' id='pwd' class='input-text password' />
        </p>
        ";

        echo "
        <p>
            <input type='submit' name='submit' value='".__( 'Login', 'jigoshop' )."' class='input-submit' />
            <a class='forgot' href='".esc_url(wp_lostpassword_url( $redirect_to ))."'>".__( 'Forgot it?', 'jigoshop' )."</a>
        </p>
        ";

        if (Jigoshop_Base::get_options()->get_option( 'jigoshop_enable_signup_form' ) == 'yes' ) {
            echo '<p class="register">';
            wp_register(__('New user?','jigoshop') . ' ' , '');
            echo '</p>';
        }

        echo "</form>";

        do_action( 'jigoshop_widget_login_after_form' );

        $links = apply_filters( 'jigoshop_widget_login_user_links', array() );

Unfortunately there's no real 'clean' way of achieving that in the scenario above using do_action() or remove_action() . The do_action() function does exactly what it sounds like it does; it marks a particular point where a certain action takes place, which you can then hook/unhook different functions to. You can run functions before and after the forms, but there's no actions being run inside the form, which limits your options.

What would actually be more useful for you is a filter , as this would let you actually affect changes to the variables/content. Alas, it looks like no filters have been applied before echoing those particular fields, so you're out of luck again.

It looks to me your only practical options are to either hide the field with CSS/JS, or fork a copy of the widget and change it as required: copy the user_login.php file to your plugins folder, rename the class name so it doesn't conflict with the existing class. Then register and load the widget:

class My_Custom_Widget_User_Login extends WP_Widget
{ 
     /**
     * Constructor
     * Setup the widget with the available options
     * Add actions to clear the cache whenever a post is saved|deleted or a theme is switched
     */
    public function __construct()
    {
        $options = array(
            'classname' => 'widget_user_login',
            'description' => __('Displays a handy login form for users', 'jigoshop')
        );
        // Ensure you also change the base ID of you widget
        parent::__construct('custom-user-login', __('Jigoshop: Login', 'jigoshop'), $options);
    }
    // Rest of class definition here; edit as you wish
} // End class

// Register and load the widget
function my_load_widget() {
    register_widget( 'custom-user-login' );
}
add_action( 'widgets_init', 'my_load_widget' );

This should give you a copy of the widget that you can tweak as you wish, separate from the rest of the Jigoshop plugin, so it won't get directly affected by updates. I haven't had a chance to test it, but hopefully it sets you on the right track.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM