2016-12-14 84 views
1

我有一个wordpress应用程序,并且在一个页面中,我需要对某个自定义PHP文件进行AJAX调用。自定义PHP代码将创建一个图像文件并将其保存在一个目录中。将图像存储在根目录的自定义目录中安全吗?wordpress - 需要添加自定义php(由AJAX调用),但不是在主题中

放置此自定义PHP文件的好地方在哪里?我应该把它放在根目录下新建的文件夹中吗?或者如果wordpress更新,它会被删除?我需要为此创建一个插件吗?

+0

可能希望检查WP插件,可以让您将PHP添加到特定页面或帖子。你到目前为止尝试过哪些步骤? – Twisty

+0

@twisty - 我还没有尝试过任何东西。所以你建议使用现有的插件......也许,我会这样做。 –

+0

是的,有一些很好的可以让你创建可以添加到帖子或页面的代码片段。这使得更新WP更容易,而无需重新创建硬编码的PHP并将其重新添加回模板等。 – Twisty

回答

1

如果您在站点的上传文件夹内使用自定义目录来存储图像,则会更好。创建它,然后chmod 775

将表单放置在页面的页面模板中。添加一个带有“upload-response”类的空白div。就在容器div的闭幕前,插入脚本标签,将回显到一个js变量管理-ajax.php路径:

<script> 
    var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>'; 
</script> 

我用jQuery来处理其余部分(该upload.js的内容文件):

(function ($) { 
$('body').on('click', '.upload-form .btn-upload', function(e){ 
    e.preventDefault(); 


    var imagedata = canvas.toDataURL('image/png'); 

    var fd = new FormData(); 
    //var files_data = imagedata; 
    fd.append('image', imagedata); 

    // our AJAX identifier 
    fd.append('action', 'my_upload_files'); 

    // Remove this code if you do not want to associate your uploads to the current page. 
    //fd.append('post_id', <?php echo $post->ID; ?>); 

    $.ajax({ 
     type: 'POST', 
     url: ajaxurl, 
     data: fd, 
     contentType: false, 
     processData: false, 
     success: function(response){ 
      $('.upload-response').html(response); // Append Server Response 
     } 
    }); 
}); 
})(jQuery); 

在你的插件文件,添加:

add_action('wp_enqueue_scripts','ajax_upload_script'); 
function ajax_upload_script() { 
    wp_enqueue_script('ajax-upload', plugins_url('/js/upload.js'), array('jquery'), '', true); 
} 

add_action('wp_ajax_my_upload_files', 'my_upload_files'); 
add_action('wp_ajax_nopriv_my_upload_files', 'my_upload_files'); // Allow front-end submission 

function my_upload_files(){ 
//file handling here 
} 

或者,如果你不想做一个插件,动作添加到您的子主题的functions.php,改变plugins_url('/js/upload.js')get_stylesheet_directory_uri().'/js/upload.js'

+0

感谢您的帮助。我不想上传文件。我想传递一个数据到php代码并让它创建png文件。所以,它必须是一个Ajax电话。 –

+0

在js中没有多少改变。我已经更新了它。 – Poldira

+0

我正在将canvas dataurl传递给它。将其更改为您需要传递的任何数据信息 – Poldira

0

一种方法是使用像PHP Code for posts这样的插件。这会比将PHP硬编码到模板或WP页面更好。优点是您可以轻松更新和更改代码,并且需要更新WP时,您不需要重新应用硬编码的PHP。

0
If you want implement ajax in wordpress than you need use wp_ajax hook. 
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action) 
add bellow code in function.php (theme folder) 


javascript add in wp_footer hook on function.php 
jQuery.post(
    ajaxurl, 
    { 
     'action': 'add_foobar', 
     'data': 'foobarid' 
    }, 
    function(response){ 
     alert('The server responded: ' + response); 
    } 
); 


bellow wp_ajax hook add on function.php 

add_action('wp_ajax_add_foobar', 'prefix_ajax_add_foobar'); 

function prefix_ajax_add_foobar() { 
    // Handle request then generate response using WP_Ajax_Response 

    // Don't forget to stop execution afterward. 
    wp_die(); 
} 


Those code will not update when you update wordpress. 
but if possible than add those code on function.php on child theme so in case if you want update theme than not update those code. 
相关问题