• Hello,

    I’ve been trying to solve this problem for a few hours now, so any help would be greatly appreciated!!

    I wrote an AJAX function to load two different pages that toggled using 2 links. Each of these pages includes native wordpress functions like get_current_user_id(). They work fine if I load them normally by entering in the address in the address bar, but when I call the page using ajax, I receive a fatal error of an undefined function.

    I’ve done a lot of searching for this on the web already, and it seems like everybody refers to the AJAX in Plugins page, which tells me to use add_action(‘wp_ajax…’,’…’). I’ve sunk many hours into trying to implement this with no success. Can anyone give me some pointers?

    Thanks,
    Ben

    Here’s my code:

    HTML:

    <ul id='learn-selector'>
            <h2>QUIZZES</h2>
            <li class='quiz-new'><a href='?learn=quiz-overview'>New Quiz</a></li>
            <li class='quiz-clinic-settings'><a href='?learn=quiz-error'>Settings</a></li>
        </ul>
    <div id='learn-body'>
    <?php
         if (isset($_GET['learn'])){
              switch ($_GET['learn']){
                  case 'quiz-overview':
                       include 'quiz-overview.php';
                  break;
                  case 'quiz-error':
                       include 'quiz-error.php';
                  break;
              }
         }
    ?>
    </div> <!-- learn-body-->

    Javascript:

    function getHTTPObject() {
    	var xhr = false;
    	if (window.XMLHttpRequest){
    			xhr = new XMLHttpRequest();
    	}
    	else if (window.ActiveXObject){
    		try{
    			xhr = new ActiveXObject('Msxml2.XMLHTTP');
    		}
    		catch(e){
    			try{
    				xhr = new ActiveXObject('Microsoft.XMLHTTP');
    			}
    			catch(e){
    				xhr = false;
    			}
    		}
    	}
    return xhr;
    }
    function grabFile(file){
    	var request = getHTTPObject();
    	if (request){
    		request.onreadystatechange = function (){
    		parseResponse(request);
    		};
    	request.open('GET',file,true);
    	request.send(null);
    	return true;
    	}
    	else {
    		return false;
    	}
    }
    window.onload = prepareLinks;
    function prepareLinks(){
    	if (!document.getElementById || !document.getElementsByTagName){
    		return;
    	}
    	var list = document.getElementById('learn-selector');
    	var links = list.getElementsByTagName('a');
    
    	for (var i=0; i<links.length;i++){
    		links[i].onclick=function(){
    			var query = this.getAttribute('href').split('?learn=')[1];
    			var url ='https://eyeguru.org/wp-content/themes/thesis_185/custom/'+query+'.php';
    			return !grabFile(url);
    		};
    	}
    }
    function parseResponse(request){
    	if (request.readyState ==4){
    		if(request.status == 200 || request.status == 304){
    			var details = document.getElementById('learn-body');
    			details.innerHTML = request.responseText;
    		}
    	}
    }

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘WordPress functions can't run within content loaded using AJAX’ is closed to new replies.