• howster

    (@howster)


    Hi Guys,

    The below procedural PHP code will add 5 text fields to Settings>General in the WP dashboard but how do I achieve the same with using OOP classes?

    function link_settings_api_init() {
    	// Add the section to general settings so we can add our
    	// fields to it
    	add_settings_section(
    	   'links_setting_section',
    	   'Social Links',
    	   'links_setting_section_callback_function',
    	   'general'
       );
    
    	// Add the field with the names and function to use for our new
    	// settings, put it in our new section
    	add_settings_field(
    	   'facebook_link',
    	   'Facebook Link',
    	   'facebook_function',
    	   'general',
    	   'links_setting_section'
       );
    
       add_settings_field(
    		'twitter_link',
    		'Twitter Link',
    		'twitter_function',
    		'general',
    		'links_setting_section'
    	);
    
    	add_settings_field(
    		'linkedin_link',
    		'Linkedn Link',
    		'linkedin_function',
    		'general',
    		'links_setting_section'
    	);
    
    	add_settings_field(
    		'instagram_link',
    		'Instagram Link',
    		'instagram_function',
    		'general',
    		'links_setting_section'
    	);
    
    	add_settings_field(
    		'youtube_link',
    		'YouTube Link',
    		'youtube_function',
    		'general',
    		'links_setting_section'
    	);
    	// Register our setting so that $_POST handling is done for us and
    	// our callback function just has to echo the <input>
    	register_setting( 'general', 'facebook_link' );
    	register_setting( 'general', 'twitter_link' );
    	register_setting( 'general', 'linkedin_link' );
    	register_setting( 'general', 'instagram_link' );
    	register_setting( 'general', 'youtube_link' );
    
    } // link_settings_api_init()
    
    add_action( 'admin_init', 'link_settings_api_init' );
    
    function links_setting_section_callback_function() {
    	echo '<p>Add Social Media Links for HSPH</p>';
    }
    
    function facebook_function() {
    $facebook_link = get_option( 'facebook_link', '' );
    echo '<input id="facebook_link" style="width: 35%;" type="text" title="Facebook Link" name="facebook_link" value="' . sanitize_text_field($facebook_link) . '" />';
    }
    
    function twitter_function() {
    $twitter_link = get_option( 'twitter_link', '' );
    echo '<input id="twitter_link" style="width: 35%;" type="text" title="Twitter Link" name="twitter_link" value="' . sanitize_text_field($twitter_link) . '" />';
    }
    
    function linkedin_function() {
    $linkedin_link = get_option( 'linkedin_link', '' );
    echo '<input id="linkedin_link" style="width: 35%;" type="text" title="LinkedIn Link" name="linkedin_link" value="' . sanitize_text_field($linkedin_link) . '" />';
    }
    
    function instagram_function() {
    $instagram_link = get_option( 'instagram_link', '' );
    echo '<input id="instagram_link" style="width: 35%;" type="text" title="Instagram Link" name="instagram_link" value="' . sanitize_text_field($instagram_link) . '" />';
    }
    
    function youtube_function() {
    $youtube_link = get_option( 'youtube_link', '' );
    echo '<input id="youtube_link" style="width: 35%;" type="text" title="YouTube Link" name="youtube_link" value="' . sanitize_text_field($youtube_link) . '" />';
    }

    How do I convert the above to work in PHP OOP

    class MY_Plugin_Social {
    
    	
    
    	public function init() {
    
    add_action( 'admin_init', 'link_settings_api_init' );
    // or should I use an array 	add_action( 'admin_init', array( $this, 'link_settings_api_init' ), 99 );
    	}
    
    public function link_settings_api_init() {
    	// Add the section to general settings so we can add our
    	// fields to it
    	add_settings_section(
    	   'links_setting_section',
    	   'Social Links',
    	   'links_setting_section_callback_function',
    	   'general'
       );
    
    	// Add the field with the names and function to use for our new
    	// settings, put it in our new section
    	add_settings_field(
    	   'facebook_link',
    	   'Facebook Link',
    	   'facebook_function',
    	   'general',
    	   'links_setting_section'
       );
    
       add_settings_field(
    		'twitter_link',
    		'Twitter Link',
    		'twitter_function',
    		'general',
    		'links_setting_section'
    	);
    
    	add_settings_field(
    		'linkedin_link',
    		'Linkedn Link',
    		'linkedin_function',
    		'general',
    		'links_setting_section'
    	);
    
    	add_settings_field(
    		'instagram_link',
    		'Instagram Link',
    		'instagram_function',
    		'general',
    		'links_setting_section'
    	);
    
    	add_settings_field(
    		'youtube_link',
    		'YouTube Link',
    		'youtube_function',
    		'general',
    		'links_setting_section'
    	);
    	// Register our setting so that $_POST handling is done for us and
    	// our callback function just has to echo the <input>
    	register_setting( 'general', 'facebook_link' );
    	register_setting( 'general', 'twitter_link' );
    	register_setting( 'general', 'linkedin_link' );
    	register_setting( 'general', 'instagram_link' );
    	register_setting( 'general', 'youtube_link' );
    
    } // link_settings_api_init()
    
    public function links_setting_section_callback_function() {
    	echo '<p>Add Social Media Links for HSPH</p>';
    }
    
    public function facebook_function() {
    $facebook_link = get_option( 'facebook_link', '' );
    echo '<input id="facebook_link" style="width: 35%;" type="text" title="Facebook Link" name="facebook_link" value="' . sanitize_text_field($facebook_link) . '" />';
    }
    
    public function twitter_function() {
    $twitter_link = get_option( 'twitter_link', '' );
    echo '<input id="twitter_link" style="width: 35%;" type="text" title="Twitter Link" name="twitter_link" value="' . sanitize_text_field($twitter_link) . '" />';
    }
    
    public function linkedin_function() {
    $linkedin_link = get_option( 'linkedin_link', '' );
    echo '<input id="linkedin_link" style="width: 35%;" type="text" title="LinkedIn Link" name="linkedin_link" value="' . sanitize_text_field($linkedin_link) . '" />';
    }
    
    public function instagram_function() {
    $instagram_link = get_option( 'instagram_link', '' );
    echo '<input id="instagram_link" style="width: 35%;" type="text" title="Instagram Link" name="instagram_link" value="' . sanitize_text_field($instagram_link) . '" />';
    }
    
    function youtube_function() {
    $youtube_link = get_option( 'youtube_link', '' );
    echo '<input id="youtube_link" style="width: 35%;" type="text" title="YouTube Link" name="youtube_link" value="' . sanitize_text_field($youtube_link) . '" />';
    }
    
    }

    I’m getting a callback error similar to Warning: call_user_func() expects parameter 1 to be a valid callback, function ‘links_setting_section_callback_function’ not found or invalid function name in /home/xyz/code/wwwhsph/wp-admin/includes/template.php on line 1608

    • This topic was modified 5 years ago by howster.
    • This topic was modified 5 years ago by howster.
    • This topic was modified 5 years ago by howster.
Viewing 5 replies - 1 through 5 (of 5 total)
  • catacaustic

    (@catacaustic)

    When you’re using objects, you need to call the function of that object, not jsut the function name, or PHP won’t know what to do – as you’ve seen now.

    Wrong way:

    add_action( 'admin_init', 'link_settings_api_init' );

    The correct way to do it is:

    add_action( 'admin_init', array ($this, 'link_settings_api_init') );

    Thread Starter howster

    (@howster)

    Thanks very much. I added add_action( 'admin_init', array ($this, 'link_settings_api_init') ); and that successfully allows the function link_settings_api_init() to execute. Now how do I get the other functions like links_setting_section_callback_function() and facebook_function()... to execute. Must I add more add_actions to public function unit() or add something to link_settings_api_init() because I’m getting errors like: Warning: call_user_func() expects parameter 1 to be a valid callback, function ‘links_setting_section_callback_function’ not found or invalid function name in /home/xyz/code/wwwhsph/wp-admin/includes/template.php on line 1608

    …I seem to find lots of info on how to use procedural PHP to do this but OOP not so much.

    MUCH APPRECIATED.

    • This reply was modified 5 years ago by howster.
    • This reply was modified 5 years ago by howster.
    • This reply was modified 5 years ago by howster.
    • This reply was modified 5 years ago by howster.
    Mark

    (@markwolters)

    You can fix it in the same way as the add action is fixed by calling the function of the object:

    Do this:

    	add_settings_section(
    	   'links_setting_section',
    	   'Social Links',
    	   array($this, 'links_setting_section_callback_function'),
    	   'general'
    

    Instead of this:

    
    	add_settings_section(
    	   'links_setting_section',
    	   'Social Links',
    	   'links_setting_section_callback_function',
    	   'general'
    
    • This reply was modified 5 years ago by Mark.
    • This reply was modified 5 years ago by Mark. Reason: typos
    • This reply was modified 5 years ago by Mark.
    Thread Starter howster

    (@howster)

    Outstanding. Thanks Mark. Exactly what I needed. I post the code that works for me.

    class myClass {
    	/**
    	 * Init function.
    	 *
    	 * @access public
    	 * @return void
    	 */
    
    	public function init()
        {
    
            add_action( 'admin_init', array( $this, 'page_init' ) );
        }
    
        public function page_init()
        {
    
    		register_setting( 'general', 'facebook_link' );
    		register_setting( 'general', 'twitter_link' );
    		register_setting( 'general', 'linkedin_link' );
    		register_setting( 'general', 'instagram_link' );
    		register_setting( 'general', 'youtube_link' );
    
            add_settings_section(
                'setting_section_id', // ID
                'Social Media Links', // Title
                array( $this, 'print_section_info' ), // Callback
                'general' // Page
            );
    
    		add_settings_field(
                'facebook_link', // ID
                'Facebook Link', // Title
                array( $this, 'facebook_callback' ), // Callback
                'general', // Page
                'setting_section_id' // Section
    		);
    		   add_settings_field(
                'twitter_link', // ID
                'Twitter Link', // Title
                array( $this, 'twitter_callback' ), // Callback
                'general', // Page
                'setting_section_id' // Section
    		);
    
    		add_settings_field(
                'instagram_link', // ID
                'Instagram Link', // Title
                array( $this, 'instagram_callback' ), // Callback
                'general', // Page
                'setting_section_id' // Section
    		);
    
    		add_settings_field(
                'linkedin_link', // ID
                'LinkedIn Link', // Title
                array( $this, 'linkedin_callback' ), // Callback
                'general', // Page
                'setting_section_id' // Section
    		);
    
    		add_settings_field(
                'youtube_link', // ID
                'YouTube Link', // Title
                array( $this, 'youtube_callback' ), // Callback
                'general', // Page
                'setting_section_id' // Section
            );
    
        }
    
        /**
         * Print the Section text
         */
        public function print_section_info()
        {
            print 'Enter social media links below:';
        }
    
    		public function facebook_callback() {
    		$facebook_link = get_option( 'facebook_link', '' );
    		echo '<input id="facebook_link" style="width: 35%;" type="text" title="Facebook Link" name="facebook_link" value="' . sanitize_text_field($facebook_link) . '" />';
    		}
    
    		public function twitter_callback() {
    		$twitter_link = get_option( 'twitter_link', '' );
    		echo '<input id="twitter_link" style="width: 35%;" type="text" title="Twitter Link" name="twitter_link" value="' . sanitize_text_field($twitter_link) . '" />';
    		}
    
    		public function linkedin_callback() {
    		$linkedin_link = get_option( 'linkedin_link', '' );
    		echo '<input id="linkedin_link" style="width: 35%;" type="text" title="LinkedIn Link" name="linkedin_link" value="' . sanitize_text_field($linkedin_link) . '" />';
    		}
    
    		public function instagram_callback() {
    		$instagram_link = get_option( 'instagram_link', '' );
    		echo '<input id="instagram_link" style="width: 35%;" type="text" title="Instagram Link" name="instagram_link" value="' . sanitize_text_field($instagram_link) . '" />';
    		}
    
    		public function youtube_callback() {
    		$youtube_link = get_option( 'youtube_link', '' );
    		echo '<input id="youtube_link" style="width: 35%;" type="text" title="YouTube Link" name="youtube_link" value="' . sanitize_text_field($youtube_link) . '" />';
    		}
    }
    • This reply was modified 5 years ago by howster.
    Thread Starter howster

    (@howster)

    Example 2 here was also very useful: https://codex.www.ads-software.com/Creating_Options_Pages

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using OOP and admin_init in Settings>General’ is closed to new replies.