• Hi everyone. I would like to be able to track a user’s preference for sounds on the website. The preference has to be available from page load to page load.

    I added an option to the funtions.php script

    add_option('audio_toggle','on','','yes');

    Next, I created a stand alone php script (raven-functions.php) to update or get a user defined option.

    <?php
    /** =================================
     *  AUDIO FUNCTIONS
     * ================================== */
    
        	if(isset($_POST['action']) && !empty($_POST['action'])) {
            	$action = $_POST['action'];
            	switch($action) {
            		case 'get_audio_toggle' :
                			get_audio_toggle();
    		            	break;
            		case 'set_audio_toggle' :
    		            	set_audio_toggle();
                			break;
            	}
        	}
    
    	function get_audio_toggle() {
    		echo get_option('audio_toggle');
    	}
    	add_action('get_option', 'get_audio_toggle');
    
    	function set_audio_toggle() {
    		if(isset($_POST['new_value']) && !empty($_POST['new_value'])) {
    			$new_value = $_POST['new_value'];
    			update_option('audio_toggle', $new_vlaue);
    			echo get_option('audio_toggle');
    		}
    	}
    	add_action('update_option', 'set_audio_toggle');
    ?>

    Finally, I created a javascript that would respond to the user clicking a ‘speaker’ image on or off on the webpage. The function in the javascript calls the both PHP functions using AJAX.

    //==========================================================
    //  AUDIO FUNCTIONS
    //==========================================================
    //--------------------------------------
      function toggleAudio() {
    //-------------------------------------- 
    
        $.get("/wordpress/wp-content/themes/LCARS/raven_functions.php", {action:'get_audio_toggle'}, function(response, status) {
            if (status === "success") {
                var myAudio = document.getElementById("raven-audio");
                var toggle = "";
                if(response === "off") {
                    $('#raven-audio-image').css('background','url(/wordpress/wp-content/themes/LCARS/images/raven-audio-on-000.png)');
                    $('#raven-audio-image').prop('title','click to turn audio off');
                    myAudio.play();
                    toggle = "on";
                }
                else {
                    $('#raven-audio-image').css('background','url(/wordpress/wp-content/themes/LCARS/images/raven-audio-off-000.png)');
                    $('#raven-audio-image').prop('title','click to turn audio on');
                    myAudio.pause();
                    myAudio.currentTime = 0;
                    toggle = "off";
                }
                $.post("/wordpress/wp-content/themes/LCARS/raven_functions.php", {action:'set_audio_toggle', new_value:toggle});
            }
        });
      }

    When I click the image button on the webpage, I am getting the following error message, “fatal error: undefined function update_option() in the php file at line 27. I am new to wordpress and read through documentation and searched for a solution. I alsso made sure that the option.php script was included as a required in the wp-settings.php file.

    I would appreciate if someone could help point me in the right direction to resolving what should be a simple requirement.

    Thank you,
    Jim Von Dolteren

Viewing 1 replies (of 1 total)
  • As you’ve discovered, you can’t AJAX directly to an arbitrary PHP file because WordPress’ functions won’t get loaded. Instead, you should $.post to ajaxurl, which automatically points to admin-ajax.php (despite the name, it works for front-end and back-end AJAX requests). Check out AJAX in Plugins for more information.

    Also, you’ve got a minor mistake in your PHP: update_option('audio_toggle', $new_vlaue); should probably be update_option('audio_toggle', $new_value); instead.

Viewing 1 replies (of 1 total)

The topic ‘Fatal Error: Undefined Function in stand alone PHP script’ is closed to new replies.