Reputation: 626
I use wordpress as a cms. I need to offer a zip file download [to registered users] on every post, containing all the images attached to the post. Given is the code only from the main file "gallery-zip.php" ( original code and rest of the files here ). The issue is it works only in localhost and NOT in the live server.
PLUGIN CODE :
<?php
namespace GalleryZip;
add_action( 'plugins_loaded', __NAMESPACE__ . '\gallery_zip_start', 10, 0 );
/**
* Invoke the plugin and load the needed classes
*/
function gallery_zip_start() {
// simple autoloader
$classes = glob( dirname( __FILE__ ) . '/classes/*.php' );
if ( ! empty( $classes ) ) {
foreach ( $classes as $class )
require_once $class;
add_action( 'init', __NAMESPACE__ . '\add_hooks', 10, 0 );
add_action( 'init', __NAMESPACE__ . '\enqueue_scripts', 10, 0 );
if ( is_admin() )
return;
// this is only needed on the frontend
GalleryZip::get_instance( new GalleryZip_DataContainer() );
}
}
/**
* Adding the needed hooks
*/
function add_hooks() {
add_action( 'wp_ajax_get_galleryzip', __NAMESPACE__ . '\get_gallery_zip', 10, 0 );
add_action( 'wp_ajax_nopriv_get_galleryzip', __NAMESPACE__ . '\get_gallery_zip', 10, 0 );
}
/**
* Enqueu the JavaScript
*/
function enqueue_scripts() {
// load minified version if SCRIPT_DEBUG is true
$min = ( defined( 'SCRIPT_DEBUG' ) && true == SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script(
'gallery-zip',
plugins_url(
sprintf( 'js/gallery_zip%s.js', $min ),
__FILE__
),
array( 'jquery' ),
false,
true
);
// set JS object with params
wp_localize_script( 'gallery-zip', 'GalleryZip', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
/**
* Ajax callback for creating the zip-file and sending the url to zip-file
*/
function get_gallery_zip() {
$send_result = function( $result = '' ) {
if ( is_array( $result ) )
$result = var_export( $result, true );
header( 'Content-type: application/json' );
die( json_encode( array( 'result' => $result ) ) );
};
$post_id = (int) filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$gallery_id = (int) filter_input( INPUT_POST, 'gallery_id', FILTER_SANITIZE_NUMBER_INT );
if ( 0 >= $post_id )
$send_result( var_export( $_POST, true ) );
$images = GalleryZip::get_images_ajax_callback( $post_id, $gallery_id );
$send_result( $images );
}
TESTING ENVIRONMENTS :
Things I tried and did.
I tested the plugin on my localhost with latest wordpress 3.6.1 and PHP version 5.3.10 and used this line of code in single.php template <?php echo do_shortcode('[gallery]'); ?>
. It offered a nice zip-download link 'Get as Zip' on click and everything was flawless.
As soon as I installed the plugin on my live site it threw an error as the site was using an old version of PHP 5.2 and specifically due to a namespace syntax in the first line of the plugin code - namespace GalleryZip;
, hence first of all I upgraded the PHP version.
Now, that I have it running on live site, it displays a link "GalleryZip" as expected on but the link does not prompt a zip-download and when clicked and just refreshes the page.
In order to rule out basic things I have tested in latest firefox, chrome and safari browsers, works as expected on localhost and not on live site. Wordpress related : I changed the theme into default twenty-thirteen ( tried it inside and outside the loop ) and also disabled all the other plugins, no change in result.
Firebug console displays http://website.com/wp-admin/admin-ajax.php 200 OK
on clicking the link, even on live site. I also asked for help a few weeks back from the original developer but have not received any response yet. So, if you may, kindly take a look at the code and let me know if this could be solved or correct me if I am wrong anywhere. Also please let me know if I should be definitely asking it somewhere else. Although I am continuously testing and in every way I can, a little helping hand is always appreciated.
Upvotes: 1
Views: 2365
Reputation: 602
Please give 775 to the directory in which you are uploading images. 777 is extremely dangerous.
Upvotes: 2