{"id":4426473,"date":"2013-12-18T16:56:43","date_gmt":"2013-12-18T16:56:43","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/remove_action-from-other-plugin-file\/"},"modified":"2016-08-21T13:39:08","modified_gmt":"2016-08-21T13:39:08","slug":"remove_action-from-other-plugin-file","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/remove_action-from-other-plugin-file\/","title":{"rendered":"remove_action from other plugin file"},"content":{"rendered":"<p>Hello all,<\/p>\n<p>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&#8217;s not being removed.<\/p>\n<p>My code is as follows:<\/p>\n<p>custom-functions.php<\/p>\n<pre><code>class Apollo_Custom_Functions {\n\tpublic function __construct() {\n\t\tadd_action( &#039;save_post&#039;, array( $this, &#039;member_title&#039; ), 10, 2 );\n\t}\n\tpublic function member_title( $postid, $post ) {\n\t\t# Can a user save a match?\n\t\tif( ! current_user_can( &#039;edit_members&#039;, $postid ) ) {\n\t\t\treturn $postid;\n\t\t}\n\n\t\t# Check if the post type is matches\n\t\tif( &#039;members&#039; !== get_post_type( $postid ) ) {\n\t\t\treturn $postid;\n\t\t}\n\n\t\t# Check the nonce\n\t\tif( ! empty( $_POST[&#039;meta_noncename_&#039; . $post-&gt;post_type] ) &amp;&amp; ! wp_verify_nonce( $_POST[&#039;meta_noncename_&#039; . $post-&gt;post_type], basename( __FILE__ ) ) ) {\n\t        return $postid;\n\t    }\n\n\t\t# Make sure it&#039;s not autosave or rivision\n\t\tif( ( defined( &#039;DOING_AUTOSAVE&#039; ) &amp;&amp; DOING_AUTOSAVE ) || $post-&gt;post_type == &#039;revision&#039; || $post-&gt;post_status == &#039;auto-draft&#039; ) {\n\t        return $postid;\n\t    }\n\n\t    if ( wp_is_post_revision( $postid ) ) {\n\t    \treturn $postid;\n\t    }\n\n\t    \/\/ unhook this function so it doesn&#039;t loop infinitely\n\t\tremove_action( &#039;save_post&#039;, array( $this, &#039;member_title&#039; ), 11 );\n\n\t\t$firstname = get_post_meta( $postid, &#039;member_first_name&#039;, true );\n\t\t$insertion = get_post_meta( $postid, &#039;member_insertion&#039;, true );\n\t\t$lastname = get_post_meta( $postid, &#039;member_last_name&#039;, true );\n\n\t\t# Set title and slug\n\t\t$member_title = $firstname . &#039; &#039; . ( ! empty( $insertion ) ? $insertion . &#039; &#039; : &#039;&#039; ) . $lastname;\n\t\t$member_slug = sanitize_title_with_dashes( $member_title, &#039;save&#039; );\n\t\t$member_slug_sanitized = sanitize_title( $member_slug );\n\n\t\t\/\/echo $member_title;\n\n\t\t$postdata = array(\n\t\t\t&#039;ID&#039; =&gt; $postid,\n\t\t\t&#039;post_title&#039; =&gt; $member_title,\n\t\t\t&#039;post_name&#039; =&gt; $member_slug_sanitized,\n\t\t);\n\n\t\twp_update_post( $postdata );\n\n\t\t\/\/ re-hook this function\n\t\tadd_action( &#039;save_post&#039;, array( $this, &#039;member_title&#039; ), 10, 2 );\n\t}\n}\n$apollo_custom_functions = new Apollo_Custom_Functions();<\/code><\/pre>\n<p>And in the import.php I want to remove this action (to avoid that it saves the post with an empty title).<\/p>\n<pre><code>class Apollo_Import_Members {\n\tpublic function add_member( $memberdata ) {\n\t\t# Remove the save post action to avoid an infinite loop.\n\t\tglobal $apollo_custom_functions;\n\t\tif(has_action(&#039;save_post&#039;, array($apollo_custom_functions, &#039;member_title&#039;))) {\n\t\t\techo &#039;yes&#039;;\n\t\t}\n\t\tremove_action( &#039;save_post&#039;, array( $apollo_custom_functions, &#039;member_title&#039; ), 99 );\n\t\t\/\/remove_all_actions( &#039;save_post&#039;, 1 );\n\t\t\/\/ Some more code here.\n}<\/code><\/pre>\n<p>I am getting a YES when I check if the action exists, but it&#8217;s not being removed. Does anyone have any idea what is going wrong here? I am totally in the dark.<\/p>\n","protected":false},"template":"","class_list":["post-4426473","topic","type-topic","status-closed","hentry","topic-tag-add_action","topic-tag-class","topic-tag-remove_action"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/4426473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/4426473\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=4426473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}