在Wordpress中从jQuery调用PHP脚本

我无法理解如何在Wordpress中使用jQuery调用php文件。</ p>
\ n

我正在尝试在Wordpress中创建一个文件上传器。 </ p>

我想在下面的函数中调用名为uploadifive.php和check-exists.php的php文件。 </ p>

控制台错误是404并且文件未到达,我还注意到jquery将wordpress永久链接附加到导致404的URL。</ p >

如何在没有自动附加到.php文件路径的永久链接的情况下调用php文件?</ p>

  jQuery.noConflict(); 

$('#file_upload')。uploadifive
({
'auto':false,
'buttonText':'Browse',
'fileSizeLimit':'1024 MB',
'ulti': false,
'queueSizeLimit':1,
'checkScript':'check-exists.php',
'queueID':'queue',
'fileType'['video / 3gpp'],
' uploadScript':'uploadifive.php',
'onUploadComplete':function(file,data){window.location ='upload_to_youtube / processing.php?'+ data;}

}};
});

</ code> </ pre>
</ div>

展开原文

原文

I'm having trouble with understanding how php files are called with jQuery in Wordpress.

I'm trying to create a file uploader within Wordpress.

I want to call the php files called uploadifive.php and check-exists.php in the function below.

The console error is a 404 and the file isn't being reached, I've also noticed that jquery is appending the wordpress permalink into the url which is causing the 404.

How can call the php file without the permalink automatically appended to the path of the .php file?

 jQuery.noConflict();

  $('#file_upload').uploadifive
      ({
          'auto'             : false,
          'buttonText'       : 'Browse',
          'fileSizeLimit'    : '1024 MB' ,
          'multi'            : false,
          'queueSizeLimit'   : 1,
          'checkScript'      : 'check-exists.php',                
          'queueID'          : 'queue',
          'fileType'     [ 'video/3gpp'],
          'uploadScript'     : 'uploadifive.php',
          'onUploadComplete' : function(file, data) {window.location =    'upload_to_youtube/processing.php?' + data;}

         });
       });   

duanchun2349
duanchun2349 wp中有几个地方你可以直接调用.php。你可以将.php文件放在wordpress根目录中,或者你可以将*.php文件放在你可以访问的主题目录中。
4 年多之前 回复
douhe4608
douhe4608 您需要了解1)AJAX,2)WordpressAJAXAPI,以及3)jQueryAJAXAPI。然后将它们连接在一起。
4 年多之前 回复

2个回答

1) first way is call your processing.php file using Ajax

$('#file_upload').uploadifive
  ({
          'auto'             : false,
          'buttonText'       : 'Browse',
          'fileSizeLimit'    : '1024 MB' ,
          'multi'            : false,
          'queueSizeLimit'   : 1,
          'checkScript'      : 'check-exists.php',                
          'queueID'          : 'queue',
          'fileType'     [ 'video/3gpp'],
          'uploadScript'     : 'uploadifive.php',
          'onUploadComplete' : function(file, data) {//window.location =    'upload_to_youtube/processing.php?' + data;
              jQuery.ajax({
                url:PUTFILEURL,
                data:data,
                type:'POST',
                success:function(data){

                }
            });


          }

  });

2) for WordPress Ajax API https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

jquery code

 var ajaxurl="/wp-admin/admin-ajax.php";
   $('#file_upload').uploadifive
  ({
          'auto'             : false,
          'buttonText'       : 'Browse',
          'fileSizeLimit'    : '1024 MB' ,
          'multi'            : false,
          'queueSizeLimit'   : 1,
          'checkScript'      : 'check-exists.php',                
          'queueID'          : 'queue',
          'fileType'     [ 'video/3gpp'],
          'uploadScript'     : 'uploadifive.php',
          'onUploadComplete' : function(file, data) {//window.location =    'upload_to_youtube/processing.php?' + data;
              jQuery.ajax({
                url:ajaxurl,
                data:data+'&action=upload_to_youtube',
                type:'POST',
                success:function(data){

                }
            });


          }

  });

php upload code put this code in function.php file

add_action('wp_ajax_upload_to_youtubee', 'upload_to_youtube_callback');//login user
  add_action('wp_ajax_nopriv_upload_to_youtube', 'upload_to_youtube_callback');//for not login user

    function upload_to_youtube_callback()
    {
        //put your upload code
    die;
    }



简单方法..但不推荐... </ p>

找到您的主题目录。</ p>


/ wp-content / themes / your-theme / </ p>
</ blockquote>

在主题目录中放入你的php文件 。 例如:你的ajax中的test.php ..将url设置成类似这样的东西.. </ p>

  $ .ajax({
url:'/ wp-content / themes / your-theme / test.php'
});
</ code> </ pre>

然后在test.php中将这些行放在上面。</ p>

< pre> define('WP_USE_THEMES',false);
require_once('../../../ wp-load.php');
</ code> </ pre>

< p>所以即使不调用主题,你仍然可以使用wordpress函数.. </ p>
</ div>

展开原文

原文

Simple way.. yet not recommended...

locate your theme directory.

/wp-content/themes/your-theme/

inside the theme directory put your php file. example: test.php.. in your ajax set the url into something like these..

$.ajax({
   url: '/wp-content/themes/your-theme/test.php'
});

then in the test.php put these lines above.

define('WP_USE_THEMES', false);
require_once('../../../wp-load.php');

so you can still use wordpress functions even without calling the theme..

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐