• Resolved Mark Jansen

    (@mark-jansen)


    Hello all,

    I am working on a custom plugin. I want to remove an action in one file, that is added in the other. But some how it’s not being removed.

    My code is as follows:

    custom-functions.php

    class Apollo_Custom_Functions {
    	public function __construct() {
    		add_action( 'save_post', array( $this, 'member_title' ), 10, 2 );
    	}
    	public function member_title( $postid, $post ) {
    		# Can a user save a match?
    		if( ! current_user_can( 'edit_members', $postid ) ) {
    			return $postid;
    		}
    
    		# Check if the post type is matches
    		if( 'members' !== get_post_type( $postid ) ) {
    			return $postid;
    		}
    
    		# Check the nonce
    		if( ! empty( $_POST['meta_noncename_' . $post->post_type] ) && ! wp_verify_nonce( $_POST['meta_noncename_' . $post->post_type], basename( __FILE__ ) ) ) {
    	        return $postid;
    	    }
    
    		# Make sure it's not autosave or rivision
    		if( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_type == 'revision' || $post->post_status == 'auto-draft' ) {
    	        return $postid;
    	    }
    
    	    if ( wp_is_post_revision( $postid ) ) {
    	    	return $postid;
    	    }
    
    	    // unhook this function so it doesn't loop infinitely
    		remove_action( 'save_post', array( $this, 'member_title' ), 11 );
    
    		$firstname = get_post_meta( $postid, 'member_first_name', true );
    		$insertion = get_post_meta( $postid, 'member_insertion', true );
    		$lastname = get_post_meta( $postid, 'member_last_name', true );
    
    		# Set title and slug
    		$member_title = $firstname . ' ' . ( ! empty( $insertion ) ? $insertion . ' ' : '' ) . $lastname;
    		$member_slug = sanitize_title_with_dashes( $member_title, 'save' );
    		$member_slug_sanitized = sanitize_title( $member_slug );
    
    		//echo $member_title;
    
    		$postdata = array(
    			'ID' => $postid,
    			'post_title' => $member_title,
    			'post_name' => $member_slug_sanitized,
    		);
    
    		wp_update_post( $postdata );
    
    		// re-hook this function
    		add_action( 'save_post', array( $this, 'member_title' ), 10, 2 );
    	}
    }
    $apollo_custom_functions = new Apollo_Custom_Functions();

    And in the import.php I want to remove this action (to avoid that it saves the post with an empty title).

    class Apollo_Import_Members {
    	public function add_member( $memberdata ) {
    		# Remove the save post action to avoid an infinite loop.
    		global $apollo_custom_functions;
    		if(has_action('save_post', array($apollo_custom_functions, 'member_title'))) {
    			echo 'yes';
    		}
    		remove_action( 'save_post', array( $apollo_custom_functions, 'member_title' ), 99 );
    		//remove_all_actions( 'save_post', 1 );
    		// Some more code here.
    }

    I am getting a YES when I check if the action exists, but it’s not being removed. Does anyone have any idea what is going wrong here? I am totally in the dark.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘remove_action from other plugin file’ is closed to new replies.