遇到了令我完全困惑的事情。
我有一个插件,其主文件如下所示:
// Global Variables
$mouldings_prefix = \'mouldings_\';
$mouldings_name = \'Mouldings\';
if(!defined(\'MOULDINGS_BASE_DIR\')) {
define(\'MOULDINGS_BASE_DIR\', dirname(__FILE__));
}
if(!defined(\'MOULDINGS_BASE_URL\')) {
define(\'MOULDINGS_BASE_URL\', plugin_dir_url(__FILE__));
}
// Includes
include(\'includes/scripts.php\'); // Conrols JS/CSS
include(\'includes/admin-post-types.php\'); // Sets up post types/taxonomies
include(\'includes/helper-functions.php\'); // Functions that extend functionality of Mouldings
include(\'includes/admin-metaboxes.php\'); // Metaboxes that appear in the admin (non-ACF)
所以没什么特别的,只需设置一些变量并包含一些帮助文件。
我有一个设置为显示(从admin-metaboxes.php
) 它会在post类型上生成一个框,称为moulding_profiles
add_action(\'admin_init\',\'roots_mouldings_meta_init\');
function roots_mouldings_meta_init()
{
// http://codex.wordpress.org/Function_Reference/add_meta_box
// add a meta box for each of the wordpress page types: posts and pages
foreach (array(\'moulding_profiles\') as $type)
{
add_meta_box(\'roots_mouldings_profile_tools\', \'Profile Tools\', \'roots_mouldings_meta_setup\', $type, \'side\', \'high\');
}
}
function roots_mouldings_meta_setup()
{
global $post;
// using an underscore, prevents the meta variable
// from showing up in the custom fields section
$meta = get_post_meta($post->ID,\'_roots_mouldings\',TRUE);
?>
<a class="button-primary" id="deleteCad" style="display:inline-block;margin:10px auto 0 auto;">Regenerate CAD File</a>
<p style="color: #999999;font-size: 11px;padding: 0;">Clicking the button above will delete the current CAD zip file created for the profile which will be regenerated upon pageload. This is useful if any of the CAD files originally listed have been modified.</p>
<?php
}
metabox本身不保存任何信息,或多或少只有一个可单击的按钮和一些描述文本。按钮本身与
scripts.php
添加到
admin.js
文件并本地化脚本,并获取PHP文件的路径(单击时调用),该文件名为
admin-delete-cad.php
// Script conrol
function mouldings_load_scripts() {
wp_enqueue_script(\'mouldings-isotope\', MOULDINGS_BASE_URL . \'js/jquery.isotope.min.js\',array(\'jquery\'),\'1.5.17\',true);
wp_enqueue_script(\'mouldings-isotope-script\', MOULDINGS_BASE_URL . \'js/script.js\',array(\'jquery\'),\'\',true);
}
add_action(\'wp_enqueue_scripts\',\'mouldings_load_scripts\');
function admin_mouldings_load_scripts() {
wp_enqueue_script(\'mouldings-admin\', MOULDINGS_BASE_URL . \'js/admin.js\',array(\'jquery\'),\'\',true);
$mouldings_admin = array(
\'deletecadfile\' => MOULDINGS_BASE_URL . \'/includes/admin-delete-cad.php\'
);
wp_localize_script( \'mouldings-admin\', \'mouldingsadmin\', $mouldings_admin);
}
add_action(\'admin_enqueue_scripts\', \'admin_mouldings_load_scripts\' );
实际数据正在正确解析到管理员,显示:
/* <![CDATA[ */
var mouldingsadmin = {"deletecadfile":"http:\\/\\/yhmags.com\\/wordpress\\/wp-content\\/plugins\\/mouldings\\/\\/includes\\/admin-delete-cad.php"};
/* ]]> */
并且正确地通过以下JS调用onclick(在
admin.js
)
jQuery(document).ready( function($) {
$(\'#deleteCad\').click(function() {
$.get(mouldingsadmin.deletecadfile, function(data) {
$(\'#deleteCad\').html(data);
});
return false;
});
});
但当我尝试访问
admin-delete-cad.php
, 我没有机会。。。我想要的任何默认WP信息(如$post->ID):
define(\'WP_BASE_DIR\', realpath(dirname(__FILE__).\'/../../../../\'));
global $post;
$id = $_GET[\'post\'];
echo $id; echo $post->ID;
echo WP_BASE_DIR . \'/cad-\'.$id.\'.zip\';
文件本身加载很好(如果我只是输入一个纯文本字符串),但正如您在上面看到的,我正在尝试获取任何帖子信息来工作。。。但什么都没有出现。我已经在这里呆了一段时间了,非常感谢您的帮助。谢谢