running into something that has got me completely puzzled.

I have a plugin with a main file that looks like this:

// 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)

So nothing too fancy, just sets up some variables and includes some helper files.

I have a metabox that is set to display (from admin-metaboxes.php) which generates a box on a post type, called moulding_profiles


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>

The metabox itself doesn't save any information, more or less just has a clickable button and some description text. The button itself is tied to a scripts.php file (also included) which adds in the admin.js file and localizes the script and gets the path to a PHP file (which would be called on-click), called 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);

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' );

The actual data is parsing to the admin correctly, showing:

/* <![CDATA[ */
var mouldingsadmin = {"deletecadfile":"http:\/\/yhmags.com\/wordpress\/wp-content\/plugins\/mouldings\/\/includes\/admin-delete-cad.php"};
/* ]]> */

and is correctly being called onclick via the following JS (in admin.js)

jQuery(document).ready( function($) {
    $('#deleteCad').click(function() {
        $.get(mouldingsadmin.deletecadfile, function(data) {
        return false;

But when I try to access data within admin-delete-cad.php, I don't have access to well... any of the default WP info (like the $post->ID) which is what I'm after:

define('WP_BASE_DIR', realpath(dirname(__FILE__).'/../../../../'));
global $post;
$id = $_GET['post'];
echo $id; echo $post->ID;
echo WP_BASE_DIR . '/cad-'.$id.'.zip';

The file itself loads fine (if I just put in a plain text string), but as you can see above, I'm trying to get any post information to work... but nothing is showing up. Been at this for a while, any help would be greatly appreciated. Thanks!

1 Answer 1


First issue- you can't access any data because your AJAX request is an entirely separate request from the one that loaded the page you're making the request from. This is not unique to WordPress. You have to pass the data you want to operate on along with your AJAX request.

Second issue- calling your plugin file directly is not the correct way to handle AJAX requests. You won't have any access to WordPress functions this way unless you manually bootstrap WordPress. Read AJAX in plugins for the correct way to handle AJAX requests in plugins.

  • Appreciate the heads up on that, used pippinsplugins.com/… as reference and got everything working. Thanks!
    – Zach
    Apr 12, 2012 at 19:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.