Forum Replies Created

Viewing 15 replies - 1 through 15 (of 19 total)
  • Thread Starter bullshark

    (@bullshark)

    I’ve tried

    I’ve done:

        $printedURL = get_sample_permalink_html($post);
        error_log(print_r($printedURL,true));
    
    	$printedURL2 = wp_ajax_sample_permalink();
    	error_log(print_r($printedURL2,true));
    
        require_once( ABSPATH . 'wp-admin/includes/post.php' );
     	$permalink = get_sample_permalink_html( $post->ID, $post->title, $post->$slug );
        error_log(print_r($permalink,true));
    
        $permalink = get_sample_permalink_html( $post->ID );
    	error_log(print_r($permalink,true));
    
        $permalink = get_preview_post_link( $post );
    	error_log(print_r($permalink,true));
    
        $permalink = get_permalink( $post_ID );
    	error_log(print_r($permalink,true));
    
        require_once ABSPATH . '/wp-admin/includes/post.php';
        error_log(print_r(get_sample_permalink_html( $post_id, $title, $slug ),true));
    
        require_once ABSPATH . '/wp-admin/includes/post.php';
        error_log(print_r(get_sample_permalink_html( $post->ID, $post->post_title),true));
    
        error_log(print_r(get_sample_permalink(),true));

    and none of these have worked.

    Thread Starter bullshark

    (@bullshark)

    @bcworkz I still got nothing.

    Thread Starter bullshark

    (@bullshark)

    alright, so now my code gives me a 400 error when I try to submit the data to the server, and a response of 0. I’ve reognized my code. My non HTML-element code is:

    My php so far is:

    //for admin-ajax
    add_action( ‘admin_enqueue_scripts’, ‘my_enqueue’ );
    function my_enqueue($hook) {
    if( ‘index.php’ != $hook ) {
    return;
    }

    wp_enqueue_script( ‘ajax-script’, plugins_url( ‘/wp-content/plugins/my-plugin/js/Controller/ajaxdata.js’, __FILE__ ), array(‘jquery’) );
    wp_localize_script( ‘ajax-script’, ‘ajax_object’,
    array( ‘ajax_url’ => admin_url( ‘admin-ajax.php’ ), ‘c’ => c ) ); //c has the data that I need to send to the remote site’s server
    }

    //relevant to admin-ajax
    add_action( ‘wp_ajax_CBAjax’, ‘CBAjax’ );

    //relevant to admin-ajax
    function CBAjax() {

    error_log(print_r($_REQUEST));

    if ( isset($_REQUEST) ) {

    $exper = $_REQUEST[‘experdata’];

    error_log(print_r($exper,true));

    echo “The exper is ” . $exper;

    }

    $body = array(
    ‘dur’ => $exper[‘experimentDuration’],
    ‘buckets’=> $experdata[‘myData’]
    );

    $response = wp_remote_post( $url = “https://subdomain.example.com:8080/api/v1/data”, array(
    ‘body’ => $body,
    ‘headers’ => array(
    ‘Authorization’ => ‘Basic ‘ . base64_encode( “[email protected]” . ‘:’ . “password!” ),
    ),
    ) );
    // I need to send my data here: “https://subdomain.example.com:8080/api/v1/data”!!!
    if($response){
    error_log(print_r($response,true));
    error_log(“PING”);

    }

    $respcode = wp_remote_retrieve_response_code($response);
    error_log(print_r($respcode,true));
    $ajax[‘remote_code’] = $response[‘response’][‘code’];
    echo json_encode( $ajax );
    error_log(print_r($ajax,true));

    wp_die();

    }

    in the creation of the metabox, I have:

    add_action( ‘admin_enqueue_scripts’, ‘my_enqueue’ );
    add_action( ‘wp_ajax_CBAjax’, ‘CBAjax’ );

    This is how I proxy the data from the button to the admin-ajax.php page:

    import {c} from ‘../submitbutton.js’;

    function grabExperimentForSending(){

    $.ajax({
    url: “admin-ajax.php”,
    type: “POST”,
    data: c ,
    success: function (response, statusCode) {
    console.log(c.exps[0]);
    console.log(statusCode is ${statusCode});
    console.log(data is ${data});
    },
    error: function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR is ${jqXHR}, textStatus is ${textStatus}, errorThrown is ${errorThrown});
    console.log(c.exps[0]);
    }
    });
    }

    and from there, it goes to ajaxdata.js

    import {c} from ‘./wordpressrelay.js’;

    function my_action_javascript(){

    $(‘#submitButton’).click( function(){
    var data = { //model
    ‘action’: ‘CBAjax’,

    ‘experdata’: ajax_object.c
    }

    jQuery.post(ajax_object.ajax_url, data, function(response) {
    console.log(‘Got this from the server: ‘ + response);
    console.log(data.experdata);
    console.log(data[experdata] is ${data['experdata']});
    });
    });

    }

    export {my_action_javascript,c};

    export {grabExperimentForSending,c};

    • This reply was modified 4 years, 5 months ago by t-p.
    • This reply was modified 4 years, 5 months ago by t-p.
    • This reply was modified 4 years, 5 months ago by t-p.
    Thread Starter bullshark

    (@bullshark)

    Hi @bcworkz, I see nothing in my error log. Are you saying it should go in the callback function given as an argument to add_action(‘admin_enqueue_scripts’,function(){})?

    However, I did not enqueue the script in which the intial JS ajax call lives, namely: import {grabDataForSending} from ‘./Controller/wordpressrelay.js’;

    That file is ./Controller/wordpressrelay.js

    import {c} from ‘../submitandcreate.js’;

    function grabDataForSending(){

    $.ajax({
    url: “post.php”,
    type: “POST”,
    data: c ,
    success: function (response, statusCode) {
    console.log(c.exps[0]);
    //console.log(response is ${response});
    console.log(statusCode is ${statusCode});
    console.log(data is ${data});
    },
    error: function(jqXHR, textStatus, errorThrown) {
    console.log(jqXHR is ${jqXHR}, textStatus is ${textStatus}, errorThrown is ${errorThrown});
    }
    });
    }

    export {grabDataForSending};

    Do I need to enqueue this? The reason I didn’t is bc the logs in there DO show up on the browser console.

    Thread Starter bullshark

    (@bullshark)

    @bcworkz I’ve tried putting what you’ve put in my code and nothing new shows up in the error log or anywhere. How can I see the value of the remote code?

    Thread Starter bullshark

    (@bullshark)

    @bcworkz can you show me how I might do this in code, now that I’ve posted code and written, so its not like I’m asking you to write my code for me. It’s just that I’m confused bc I barely have written WordPress and the mini examples online are just that; mini.

    I want a status code from sending the data to https://subdomain.example.com:9003/api/v1/experiment, not from sending it from the browser to admin-ajax.

    Thread Starter bullshark

    (@bullshark)

    my php code is

    function my_action_javascript() { ?>
            <script type="text/javascript" >
            jQuery(document).ready(function($) {
                
                var data = {
                    'action': 'cbAjax',
                    'experdata': c.exps[0]
                };
                
                
                
                jQuery.post(ajaxurl, data, function(response) {
                    console.log('Got this from the server: ' + response);
                    console.log(data.experdata);
                    console.log(<code>data[experdata] is ${data['experdata']}</code>);
                });
            });
            </script> <?php
            error_log(print_r(serialize($_GET['experdata']),true));
        }
        
    
        function cbAjax() {
    
            error_log(print_r($_REQUEST));
             
             if ( isset($_REQUEST) ) {
    
                 $exper = $_REQUEST['experdata'];
    
                 error_log(print_r($exper,true));
    
                 
                 echo "The exper is " . $exper;
    
             }
    
             
             $body = array(
                 'dur'    => $exper['experimentDuration'],
                 'buckets'=> $experdata['headlineBuckets']
             );
    
             $response = wp_remote_post( $url = "https://subdomain.example.com:9003/api/v1/experiment", array(
                 'body'    => $body,
                 'headers' => array(
                     'Authorization' => 'Basic ' . base64_encode( "[email protected]" . ':' . "mypassword" ),
                 ),
             ) );
    
             if($response){
                error_log(print_r($response,true));
                error_log("PING");
             }
             
            wp_die();
    
        }
    in the creation of the metabox, I have:
    
        add_action( 'wp_ajax_my_action_javascript', 'my_action_javascript' ); // Write our JS below here
        add_action( 'wp_ajax_cbAjax', 'cbAjax' );

    Thing is: how do I get a status code from the server so I can know if any data was transferred at all to the server?

    Thread Starter bullshark

    (@bullshark)

    @bcworkz where do I write/define the ajaxurl?

    Thread Starter bullshark

    (@bullshark)

    @bcworkz with the code I’ve provided, what might what I need to do look like?

    Thread Starter bullshark

    (@bullshark)

    @bcworkz the remote server than the logged in WP user needs to send data to does need a password. I need the WP user’s browser to make a request to WordPress; the hook I need to write must receive that AJAX call, and in turn makes a request to the remote server, then returns the result to the WordPress user.

    Thread Starter bullshark

    (@bullshark)

    the remote server does need a password. Php code needs to interface with that server.

    Thread Starter bullshark

    (@bullshark)

    @bcworkz the authentication deets should not need to be entered for the remote server. The user has already logged into wordpress at the point of using this plugin. I’m just confused what a proper WP Ajax request might look like at this point, and in relation to the code I’ve provided. Please help me flesh it out. Thanks.

    The AJAX hook I need to make has been described as (tho I don’t know what it truly means) as that receives instructions from the browser, and uses those instructions to make a request to the remote server.

    • This reply was modified 4 years, 5 months ago by bullshark.
    Thread Starter bullshark

    (@bullshark)

    So now I have:

    	function meta_box_setup() {
    		add_action( 'add_meta_boxes', __NAMESPACE__ . '\add_meta_box' );
    		add_action( 'save_post', __NAMESPACE__ . '\save_meta_box_data', 10, 2 );
    
    		wp_register_script('Module3','/wp-content/plugins/my-plugin/js/Controller/module3.js');
    		wp_register_script('Module2','/wp-content/plugins/my-plugin/js/model/module2.js');
    		wp_register_script('Module1','/wp-content/plugins/my-plugin/js/module1.js');
    		add_action( 'admin_enqueue_scripts', function () {
    			wp_enqueue_script('Module3');
    			wp_enqueue_script('Module2');
    			wp_enqueue_script('Module1');
    		});
    		add_filter('script_loader_tag', 'add_type_attribute' , 10, 2);
    		
    		add_action( 'admin_enqueue_scripts', function () {
    			error_log("inside the scripts callback");
    			wp_enqueue_script('HTMLelement1','/wp-content/plugins/my-plugin/js/file1.js');
    			wp_enqueue_script('HTMLelement2','/wp-content/plugins/my-plugin/js/file2.js');
    			wp_enqueue_script('HTMLelement3','/wp-content/plugins/my-plugin/js/file3.js');
    			wp_enqueue_script('HTMLelement4','/wp-content/plugins/my-plugin/js/file4.js');
    		});
    	}
    
    	
    	function add_type_attribute($tag, $handle) {
    		error_log("now inside the function that should turn scripts to modules");
    	    if (( 'Module1' !== $handle ) || ('Module2' !== $handle) ||('Module3' !== $handle)) {
    	        return $tag;
    	    }
    	    elseif ( 'Module1' === $handle){
    	    	$tag = '<script type="module" src="/wp-content/plugins/my-plugin/js/module1.js"></script>';
    	    	return $tag;
    	    }
    	    elseif ('Module3' === $handle){
    	    	$tag = '<script type="module" src="/wp-content/plugins/my-plugin/js/Controller/module2.js"></script>';
    	    	return $tag;
    	    }
    	    elseif('Module2' === $handle){
    	    	$tag = '<script type="module" src="/wp-content/plugins/my-plugin/js/model/module3.js"></script>';
    	    	return $tag;
    	    }	
    	}

    and the css on the post page is broken and NONE of the scripts load. Please help me? I’ve tried to listen to @bcworkz by registering the scripts, using the callback to put module in, and then enqueuing them to no avail.

    • This reply was modified 4 years, 6 months ago by Jan Dembowski.
    • This reply was modified 4 years, 6 months ago by bcworkz. Reason: code fixed
    Thread Starter bullshark

    (@bullshark)

    @bcworkz thank you; can you give me a code snippet (relative to the code I’ve already shown in this thread) of how I should get this accomplished? That way, if/when it works, I can see the difference in what I see’s relation to the code changes I make? Thank you.

    Thread Starter bullshark

    (@bullshark)

    the way I’m trying to load the scripts, not working, is this:

    	wp_register_script('Module1', '/wp-content/plugins/my-plugin/js/submitandcreate.js');
    	wp_register_script('Module2', '/wp-content/plugins/my-plugin/js/model/modelobject.js');
    	wp_register_script('Module3', '/wp-content/plugins/my-plugin/js/controller/captainfreedom.js');
    
    	add_filter('script_loader_tag', 'add_type_attribute' , 10, 2);
    
    	function add_type_attribute($tag, $handle) {
    	    if (( 'Module1' !== $handle ) || ('Module2' !== $handle) ||('Module3' !== $handle)) {
    	        return $tag;
    	    }
    	    elseif ( 'Module1' === $handle){
    	    	$src = '/wp-content/plugins/my-plugin/js/module1.js';
    	    	$tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
    	    }
    	    elseif ('Module2' === $handle){
    	    	$src = '/wp-content/plugins/my-plugin/js/Controller/module2.js';
    	    	$tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
    	    }
    	    elseif('Module3' === $handle){
    	    	$src = '/wp-content/plugins/my-plugin/js/model/module3.js';
    	    	$tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
    	    }	
    	    return $tag;
    	}

    note: in the snippet above and in the OP

    not only is this not working, but none of these scripts show up when I look at “view source” but when I look at the console, I see Reference errors claiming other dependencies that are used in OTHER plugins are not defined.

    • This reply was modified 4 years, 6 months ago by Jan Dembowski.
    • This reply was modified 4 years, 6 months ago by bcworkz. Reason: code fixed
Viewing 15 replies - 1 through 15 (of 19 total)