0

for a plugin i am creating i want to execute a function only when the specific options is updated to save ressources. It works so far, but the hook update_option_{$option} should normaly be fired after the option is saved. In my case the old value is called all the time. $recheck is a boolean that is always false except another submit button is used.

public function __construct($recheck) {
        parent::__construct();
        $this->options = get_option( 'privacy_cf7' ); 
        $this->recheck = $recheck;

        if ($this->recheck)
            $this->ph_cf7_privacy();

        add_action('update_option_privacy_cf7', array($this, 'ph_cf7_privacy'), 11, 0);
    }

    public function ph_cf7_privacy() {

        if ( $this->ph_cf7_check_cf7_installation() && !empty( $this->ph_cf7_find_contact_forms() ) ) {

            //get all forms with cf7 post_type
            $myposts = $this->ph_cf7_find_contact_forms();

            //get option from settings page
            $acceptance         = $this->options['message'];
            $acceptanceReplaced = $this->ph_replace_placeholder( $acceptance );


            //iterate through found forms
            foreach ( $myposts as $post ) {

                $post_id        = $post->ID;
                $formContent    = get_post_meta($post_id, '_form', true);

                if($this->recheck) {
                    $this->ph_cf7_add_acceptance( $formContent, $acceptanceReplaced, $post_id );
                }

                if(!$this->recheck) {
                    $this->ph_cf7_update_acceptance( $formContent, $acceptanceReplaced, $post_id );
                }

                //Add Checkbox to mail, if form already contains checkbox
                $formContent    = get_post_meta($post_id, '_form', true);
                if ($this->ph_cf7_check_acceptance( $formContent )) {
                    $this->ph_cf7_add_acceptance_mail( $post_id );
                }

            } //end iteration

        } //end check_cf7_installation() && find_contact_forms()

    } //end function

This is how the instance is created:

//Fire when Plugins are loaded
add_action( 'plugins_loaded', 'create_ph_instances_wp_loaded' );
function create_ph_instances_wp_loaded() {

    //Instance for Privacy Contact Form 7
    //@Param boolean Check all CF7 Forms and insert the snippet
    $privacyCF7 = new PrivacyHelperCF7(false);
}

To illustrate the options is e.g. storing Strings:

Status1 | Status2 | Options used in update_option_{$option}


"test" | "test2" | "test"

I checked back that the options are saved correctly in the databse.

I would appreciate any help with this.

  • FWIW: You call $this->ph_cf7_find_contact_forms(); twice. Not sure those have side effects? Maybe it runs a WP_Query reloading Posts? And what is empty? – Clemens Tolboom May 24 '18 at 10:35
  • The function checks for wordpress posts with post_type wpcf7_contact_form . It only returns the post list. Before i added the update_option_{$option} i simply called $this->ph_cf7_privacy(); directly in the constructor. That worked like a charm but also fires on every page load. – JUVO_Webdes May 24 '18 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.