Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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
}

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

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) {
            $('#deleteCad').html(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!

share|improve this question

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.

share|improve this answer
    
Appreciate the heads up on that, used pippinsplugins.com/… as reference and got everything working. Thanks! –  Zach Apr 12 '12 at 19:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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