0

please help me with this i've been working on it for 2. I am sorry if the title isn't the exact explanation of the problem but i just can't figure out how to handle this problem. I created a php plugin but the php file isn't able to call any type of wordpress function example - (add_action). at first it was giving me an error saying function undefined. After giving giving it the location of the wp-load.php file it stoped giving me the error but it still isn't loading anything called with add_action. What do you think is causing this problem.

<?php 
   $parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
   require_once( $parse_uri[0] . 'wp-load.php' );
   add_action('init','addText');
   function addText(){
     echo "hello world";
   }
?>

Also i am loading this from the tinymce editor. This is the tinymce button code it's only job is to load the php file. I know that i am not calling ajax in the correct way but this is the only way i can call it because it is directly loaded from my javascript file.

(function() {
    tinymce.PluginManager.add('ImageSlider', function( editor, url ) {
function customfunction() {
    jQuery(document).ready( function($) {
        var dir1 = "../wp-content/plugins/Midnight Image-Slider/MDslider.php";
        $.ajax({
            type:"POST",
            url: dir1,
            success: function(data) {
                $('.mce-window-body').append(data);
            }
        })
    })
}
editor.addButton( 'ImageSlider', {
    title: 'My test button',
    image: url + '/lion.jpg',
    onclick: function(){
        editor.windowManager.open({
            title: 'Image slider',
            width:800,
            height:450,
            body:[
                customfunction()                
            ],
            onSubmit: function(e) {


            }
        })
    }
});
});
})(); 
  • Why are you unable to use the admin-ajax URL to handle this? – Milo Apr 18 '17 at 13:10
  • i can't localize them because i have no php file with which to call it – iani garthalski Apr 18 '17 at 13:13
  • I'm not sure how you'd correctly pass the URL to your file dynamically, but you can hardcode the path to admin-ajax.php exactly the same way you're now pointing to your plugin file. – Milo Apr 18 '17 at 13:20
  • i don't know how to do this. this is the path to admin-ajax.php '..wp-admin/admin-ajax.php' . How will it be able to reach my php file – iani garthalski Apr 18 '17 at 13:37
  • Read https://codex.wordpress.org/AJAX_in_Plugins to learn how. You could also use the REST API to do this, which may be a better choice. Both of these things have been discussed here a lot if you search around. – Milo Apr 18 '17 at 15:37

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.