I am using the default thumbnail feature of Wordpress 3 to add thumbnails of various sizes to my posts. Now the problem I face is that the shrinked thumbnails become very blurry.
In .Net I know how to sharpen images and this works really well for thumbnails, so I am now looking for a way to do this for Wordpress (in php obviously).
I really suck at php, so any help is really appreciated!
I use a modified version of ResizeThumbnails. This way you can re-generate all your blurry images and it also takes care of future uploaded images:
<?php
/*
**************************************************************************
Plugin Name: Regenerate Thumbnails
Plugin URI: http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
Description: Allows you to regenerate all thumbnails after changing the thumbnail sizes.
Version: 2.2.3
Author: Viper007Bond
Author URI: http://www.viper007bond.com/
**************************************************************************
Copyright (C) 2008-2011 Viper007Bond
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
class RegenerateThumbnails {
var $menu_id;
// Plugin initialization
function RegenerateThumbnails() {
// Load up the localization file if we're using WordPress in a different language
// Place it in this plugin's "localization" folder and name it "regenerate-thumbnails-[value in wp-config].mo"
load_plugin_textdomain( 'regenerate-thumbnails', false, '/regenerate-thumbnails/localization' );
add_action( 'admin_menu', array( &$this, 'add_admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueues' ) );
add_action( 'wp_ajax_regeneratethumbnail', array( &$this, 'ajax_process_image' ) );
add_filter( 'media_row_actions', array( &$this, 'add_media_row_action' ), 10, 2 );
//add_filter( 'bulk_actions-upload', array( &$this, 'add_bulk_actions' ), 99 ); // A last minute change to 3.1 makes this no longer work
add_action( 'admin_head-upload.php', array( &$this, 'add_bulk_actions_via_javascript' ) );
add_action( 'admin_action_bulk_regenerate_thumbnails', array( &$this, 'bulk_action_handler' ) ); // Top drowndown
add_action( 'admin_action_-1', array( &$this, 'bulk_action_handler' ) ); // Bottom dropdown (assumes top dropdown = default value)
// Allow people to change what capability is required to use this plugin
$this->capability = apply_filters( 'regenerate_thumbs_cap', 'manage_options' );
}
// Register the management page
function add_admin_menu() {
$this->menu_id = add_management_page( __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ), __( 'Regen. Thumbnails', 'regenerate-thumbnails' ), $this->capability, 'regenerate-thumbnails', array(&$this, 'regenerate_interface') );
}
// Enqueue the needed Javascript and CSS
function admin_enqueues( $hook_suffix ) {
if ( $hook_suffix != $this->menu_id )
return;
// WordPress 3.1 vs older version compatibility
if ( wp_script_is( 'jquery-ui-widget', 'registered' ) )
wp_enqueue_script( 'jquery-ui-progressbar', plugins_url( 'jquery-ui/jquery.ui.progressbar.min.js', __FILE__ ), array( 'jquery-ui-core', 'jquery-ui-widget' ), '1.8.6' );
else
wp_enqueue_script( 'jquery-ui-progressbar', plugins_url( 'jquery-ui/jquery.ui.progressbar.min.1.7.2.js', __FILE__ ), array( 'jquery-ui-core' ), '1.7.2' );
wp_enqueue_style( 'jquery-ui-regenthumbs', plugins_url( 'jquery-ui/redmond/jquery-ui-1.7.2.custom.css', __FILE__ ), array(), '1.7.2' );
}
// Add a "Regenerate Thumbnails" link to the media row actions
function add_media_row_action( $actions, $post ) {
if ( 'image/' != substr( $post->post_mime_type, 0, 6 ) || ! current_user_can( $this->capability ) )
return $actions;
$url = wp_nonce_url( admin_url( 'tools.php?page=regenerate-thumbnails&goback=1&ids=' . $post->ID ), 'regenerate-thumbnails' );
$actions['regenerate_thumbnails'] = '' . __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ) . '';
return $actions;
}
// Add "Regenerate Thumbnails" to the Bulk Actions media dropdown
function add_bulk_actions( $actions ) {
$delete = false;
if ( ! empty( $actions['delete'] ) ) {
$delete = $actions['delete'];
unset( $actions['delete'] );
}
$actions['bulk_regenerate_thumbnails'] = __( 'Regenerate Thumbnails', 'regenerate-thumbnails' );
if ( $delete )
$actions['delete'] = $delete;
return $actions;
}
// Add new items to the Bulk Actions using Javascript
// A last minute change to the "bulk_actions-xxxxx" filter in 3.1 made it not possible to add items using that
function add_bulk_actions_via_javascript() {
if ( ! current_user_can( $this->capability ) )
return;
?>
<script type="text/javascript">
jQuery(document).ready(function($){
$('select[name^="action"] option:last-child').before('<option value="bulk_regenerate_thumbnails"><?php echo esc_attr( __( 'Regenerate Thumbnails', 'regenerate-thumbnails' ) ); ?></option>');
});
</script>
<?php
}
// Handles the bulk actions POST
function bulk_action_handler() {
if ( empty( $_REQUEST['action'] ) || ( 'bulk_regenerate_thumbnails' != $_REQUEST['action'] && 'bulk_regenerate_thumbnails' != $_REQUEST['action2'] ) )
return;
if ( empty( $_REQUEST['media'] ) || ! is_array( $_REQUEST['media'] ) )
return;
check_admin_referer( 'bulk-media' );
$ids = implode( ',', array_map( 'intval', $_REQUEST['media'] ) );
// Can't use wp_nonce_url() as it escapes HTML entities
wp_redirect( add_query_arg( '_wpnonce', wp_create_nonce( 'regenerate-thumbnails' ), admin_url( 'tools.php?page=regenerate-thumbnails&goback=1&ids=' . $ids ) ) );
exit();
}
// The user interface plus thumbnail regenerator
function regenerate_interface() {
global $wpdb;
?>
<div id="message" class="updated fade" style="display:none"></div>
<div class="wrap regenthumbs">
<h2><?php _e('Regenerate Thumbnails', 'regenerate-thumbnails'); ?></h2>
<?php
// If the button was clicked
if ( ! empty( $_POST['regenerate-thumbnails'] ) || ! empty( $_REQUEST['ids'] ) ) {
// Capability check
if ( ! current_user_can( $this->capability ) )
wp_die( __( 'Cheatin’ uh?' ) );
// Form nonce check
check_admin_referer( 'regenerate-thumbnails' );
// Create the list of image IDs
if ( ! empty( $_REQUEST['ids'] ) ) {
$images = array_map( 'intval', explode( ',', trim( $_REQUEST['ids'], ',' ) ) );
$ids = implode( ',', $images );
} else {
// Directly querying the database is normally frowned upon, but all
// of the API functions will return the full post objects which will
// suck up lots of memory. This is best, just not as future proof.
if ( ! $images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC" ) ) {
echo ' <p>' . sprintf( __( "Unable to find any images. Are you sure <a href='%s'>some exist</a>?", 'regenerate-thumbnails' ), admin_url( 'upload.php?post_mime_type=image' ) ) . "</p></div>";
return;
}
// Generate the list of IDs
$ids = array();
foreach ( $images as $image )
$ids[] = $image->ID;
$ids = implode( ',', $ids );
}
echo ' <p>' . __( "Please be patient while the thumbnails are regenerated. This can take a while if your server is slow (inexpensive hosting) or if you have many images. Do not navigate away from this page until this script is done or the thumbnails will not be resized. You will be notified via this page when the regenerating is completed.", 'regenerate-thumbnails' ) . '</p>';
$count = count( $images );
$text_goback = ( ! empty( $_GET['goback'] ) ) ? sprintf( __( 'To go back to the previous page, click here.', 'regenerate-thumbnails' ), 'javascript:history.go(-1)' ) : '';
$text_failures = sprintf( __( 'All done! %1$s image(s) were successfully resized in %2$s seconds and there were %3$s failure(s). To try regenerating the failed images again, click here. %5$s', 'regenerate-thumbnails' ), "' + rt_successes + '", "' + rt_totaltime + '", "' + rt_errors + '", esc_url( wp_nonce_url( admin_url( 'tools.php?page=regenerate-thumbnails&goback=1' ), 'regenerate-thumbnails' ) . '&ids=' ) . "' + rt_failedlist + '", $text_goback );
$text_nofailures = sprintf( __( 'All done! %1$s image(s) were successfully resized in %2$s seconds and there were 0 failures. %3$s', 'regenerate-thumbnails' ), "' + rt_successes + '", "' + rt_totaltime + '", $text_goback );
?>
<noscript><p><em><?php _e( 'You must enable Javascript in order to proceed!', 'regenerate-thumbnails' ) ?></em></p></noscript>
<div id="regenthumbs-bar" style="position:relative;height:25px;">
<div id="regenthumbs-bar-percent" style="position:absolute;left:50%;top:50%;width:300px;margin-left:-150px;height:25px;margin-top:-9px;font-weight:bold;text-align:center;"></div>
</div>
<p><input type="button" class="button hide-if-no-js" name="regenthumbs-stop" id="regenthumbs-stop" value="<?php _e( 'Abort Resizing Images', 'regenerate-thumbnails' ) ?>" /></p>
<h3 class="title"><?php _e( 'Debugging Information', 'regenerate-thumbnails' ) ?></h3>
<p>
<?php printf( __( 'Total Images: %s', 'regenerate-thumbnails' ), $count ); ?><br />
<?php printf( __( 'Images Resized: %s', 'regenerate-thumbnails' ), '<span id="regenthumbs-debug-successcount">0</span>' ); ?><br />
<?php printf( __( 'Resize Failures: %s', 'regenerate-thumbnails' ), '<span id="regenthumbs-debug-failurecount">0</span>' ); ?>
</p>
<ol id="regenthumbs-debuglist">
<li style="display:none"></li>
</ol>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function($){
var i;
var rt_images = [<?php echo $ids; ?>];
var rt_total = rt_images.length;
var rt_count = 1;
var rt_percent = 0;
var rt_successes = 0;
var rt_errors = 0;
var rt_failedlist = '';
var rt_resulttext = '';
var rt_timestart = new Date().getTime();
var rt_timeend = 0;
var rt_totaltime = 0;
var rt_continue = true;
// Create the progress bar
$("#regenthumbs-bar").progressbar();
$("#regenthumbs-bar-percent").html( "0%" );
// Stop button
$("#regenthumbs-stop").click(function() {
rt_continue = false;
$('#regenthumbs-stop').val("<?php echo $this->esc_quotes( __( 'Stopping...', 'regenerate-thumbnails' ) ); ?>");
});
// Clear out the empty list element that's there for HTML validation purposes
$("#regenthumbs-debuglist li").remove();
// Called after each resize. Updates debug information and the progress bar.
function RegenThumbsUpdateStatus( id, success, response ) {
$("#regenthumbs-bar").progressbar( "value", ( rt_count / rt_total ) * 100 );
$("#regenthumbs-bar-percent").html( Math.round( ( rt_count / rt_total ) * 1000 ) / 10 + "%" );
rt_count = rt_count + 1;
if ( success ) {
rt_successes = rt_successes + 1;
$("#regenthumbs-debug-successcount").html(rt_successes);
$("#regenthumbs-debuglist").append("<li>" + response.success + "</li>");
}
else {
rt_errors = rt_errors + 1;
rt_failedlist = rt_failedlist + ',' + id;
$("#regenthumbs-debug-failurecount").html(rt_errors);
$("#regenthumbs-debuglist").append("<li>" + response.error + "</li>");
}
}
// Called when all images have been processed. Shows the results and cleans up.
function RegenThumbsFinishUp() {
rt_timeend = new Date().getTime();
rt_totaltime = Math.round( ( rt_timeend - rt_timestart ) / 1000 );
$('#regenthumbs-stop').hide();
if ( rt_errors > 0 ) {
rt_resulttext = '<?php echo $text_failures; ?>';
} else {
rt_resulttext = '<?php echo $text_nofailures; ?>';
}
$("#message").html("<p><strong>" + rt_resulttext + "</strong></p>");
$("#message").show();
}
// Regenerate a specified image via AJAX
function RegenThumbs( id ) {
$.ajax({
type: 'POST',
url: ajaxurl,
data: { action: "regeneratethumbnail", id: id },
success: function( response ) {
if ( response.success ) {
RegenThumbsUpdateStatus( id, true, response );
}
else {
RegenThumbsUpdateStatus( id, false, response );
}
if ( rt_images.length && rt_continue ) {
RegenThumbs( rt_images.shift() );
}
else {
RegenThumbsFinishUp();
}
},
error: function( response ) {
RegenThumbsUpdateStatus( id, false, response );
if ( rt_images.length && rt_continue ) {
RegenThumbs( rt_images.shift() );
}
else {
RegenThumbsFinishUp();
}
}
});
}
RegenThumbs( rt_images.shift() );
});
// ]]>
</script>
<?php
}
// No button click? Display the form.
else {
?>
<form method="post" action="">
<?php wp_nonce_field('regenerate-thumbnails') ?>
<p><?php printf( __( "Use this tool to regenerate thumbnails for all images that you have uploaded to your blog. This is useful if you've changed any of the thumbnail dimensions on the <a href='%s'>media settings page</a>. Old thumbnails will be kept to avoid any broken images due to hard-coded URLs.", 'regenerate-thumbnails' ), admin_url( 'options-media.php' ) ); ?></p>
<p><?php printf( __( "You can regenerate specific images (rather than all images) from the <a href='%s'>Media</a> page. Hover over an image's row and click the link to resize just that one image or use the checkboxes and the "Bulk Actions" dropdown to resize multiple images (WordPress 3.1+ only).", 'regenerate-thumbnails '), admin_url( 'upload.php' ) ); ?></p>
<p><?php _e( "Thumbnail regeneration is not reversible, but you can just change your thumbnail dimensions back to the old values and click the button again if you don't like the results.", 'regenerate-thumbnails' ); ?></p>
<p><?php _e( 'To begin, just press the button below.', 'regenerate-thumbnails '); ?></p>
<p><input type="submit" class="button hide-if-no-js" name="regenerate-thumbnails" id="regenerate-thumbnails" value="<?php _e( 'Regenerate All Thumbnails', 'regenerate-thumbnails' ) ?>" /></p>
<noscript><p><em><?php _e( 'You must enable Javascript in order to proceed!', 'regenerate-thumbnails' ) ?></em></p></noscript>
</form>
<?php
} // End if button
?>
</div>
<?php
}
// Process a single image ID (this is an AJAX handler)
function ajax_process_image() {
#error_reporting( 0 ); // Don't break the JSON result
header( 'Content-type: application/json' );
$id = (int) $_REQUEST['id'];
$image = get_post( $id );
if ( ! $image || 'attachment' != $image->post_type || 'image/' != substr( $image->post_mime_type, 0, 6 ) )
die( json_encode( array( 'error' => sprintf( __( 'Failed resize: %s is an invalid image ID.', 'regenerate-thumbnails' ), esc_html( $_REQUEST['id'] ) ) ) ) );
if ( ! current_user_can( $this->capability ) )
$this->die_json_error_msg( $image->ID, __( "Your user account doesn't have permission to resize images", 'regenerate-thumbnails' ) );
$fullsizepath = get_attached_file( $image->ID );
if ( false === $fullsizepath || ! file_exists( $fullsizepath ) )
$this->die_json_error_msg( $image->ID, sprintf( __( 'The originally uploaded image file cannot be found at %s', 'regenerate-thumbnails' ), '<code>' . esc_html( $fullsizepath ) . '</code>' ) );
#set_time_limit( 900 ); // 5 minutes per image should be PLENTY
$metadata = wp_generate_attachment_metadata( $image->ID, $fullsizepath );
if ( is_wp_error( $metadata ) )
$this->die_json_error_msg( $image->ID, $metadata->get_error_message() );
if ( empty( $metadata ) )
$this->die_json_error_msg( $image->ID, __( 'Unknown failure reason.', 'regenerate-thumbnails' ) );
// If this fails, then it just means that nothing was changed (old value == new value)
wp_update_attachment_metadata( $image->ID, $metadata );
die( json_encode( array( 'success' => sprintf( __( '"%1$s" (ID %2$s) was successfully resized in %3$s seconds.', 'regenerate-thumbnails' ), esc_html( get_the_title( $image->ID ) ), $image->ID, timer_stop() ) ) ) );
}
// Helper to make a JSON error message
function die_json_error_msg( $id, $message ) {
die( json_encode( array( 'error' => sprintf( __( '"%1$s" (ID %2$s) failed to resize. The error message was: %3$s', 'regenerate-thumbnails' ), esc_html( get_the_title( $id ) ), $id, $message ) ) ) );
}
// Helper function to escape quotes in strings for use in Javascript
function esc_quotes( $string ) {
return str_replace( '"', '\"', $string );
}
}
// Start up this plugin
add_action( 'init', 'RegenerateThumbnails' );
function RegenerateThumbnails() {
global $RegenerateThumbnails;
$RegenerateThumbnails = new RegenerateThumbnails();
}
// sharpen uploaded images
function ajx_sharpen_resized_files( $resized_file ) {
$image = wp_load_image( $resized_file );
if ( !is_resource( $image ) )
return new WP_Error( 'error_loading_image', $image, $file );
$size = #getimagesize( $resized_file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
switch ( $orig_type ) {
case IMAGETYPE_JPEG:
$matrix = array(
array(apply_filters('sharpen_resized_corner',-1.2), apply_filters('sharpen_resized_side',-1), apply_filters('sharpen_resized_corner',-1.2)),
array(apply_filters('sharpen_resized_side',-1), apply_filters('sharpen_resized_center',20), apply_filters('sharpen_resized_side',-1)),
array(apply_filters('sharpen_resized_corner',-1.2), apply_filters('sharpen_resized_side',-1), apply_filters('sharpen_resized_corner',-1.2)),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($image, $matrix, $divisor, $offset);
imagejpeg($image, $resized_file,apply_filters( 'jpeg_quality', 100, 'edit_image' ));
break;
case IMAGETYPE_PNG:
return $resized_file;
case IMAGETYPE_GIF:
return $resized_file;
}
// we don't need images in memory anymore
imagedestroy( $image );
return $resized_file;
}
add_filter('image_make_intermediate_size', 'ajx_sharpen_resized_files', 900);
?>
i think this is your answer: http://vikjavev.no/computing/ump.php ,
however you'll need to change the thumbnailer creator script in wp.
Here is the solution if your wordpress post thumbnail blurry, go to your theme CSS , get the code :
.secondary-posts .post-thumbnail
width: 100%;
background-color: #666;
height: 150px;
padding: 0;
box-shadow: 0 0px 5px #888;
background-repeat: no-repeat;
background-position:center;
background-size: 150px auto;
( CHANGE THIS BACKGROUND SIZE TO YOUR THUMBNAIL BOX SIZE( example 150px )
I have used it for our web and its works ,the thumbnail sharp now,here is the result
: http://kresnatourjogja.com/category/jogjakarta-temple/
Related
I'm using a theme which has an ajax filter for the product category pages, and it works really well.
The only thing is that the button wish is displayed, when someone is selecting filters, don't reset them, it directs the visitor back to the shop page. How can I change the logic of the button, that it would just reset the selected filters? So that the users stay on the page where they are? Because right now the button makes no sense.
Thx so far.
HTML
<div class="nm-shop-results-bar has-filters is-category">
<a href="http://localhost/amaoni/" id="nm-shop-filters-reset" data-shop-url="http://localhost/amaoni/">
<i class="nm-font nm-font-close2"></i>Filters active <span>(1)</span></a>
<a href="http://localhost/amaoni/" id="nm-shop-search-taxonomy-reset" data-shop-url="http://localhost/amaoni/"
<i class="nm-font nm-font-close2"></i>Showing <span>“Hundebetten”</span></a>
</div>
PHP
<?php
/**
* NM: Shop - Results bar/button
*/
defined( 'ABSPATH' ) || exit;
global $nm_theme_options;
$results_bar_class = '';
$results_bar_buttons = array();
// Filters
$filters_count = nm_get_active_filters_count();
if ( $filters_count ) {
$results_bar_class = ' has-filters';
$results_bar_buttons['filters'] = array(
'id' => 'nm-shop-filters-reset',
'title' => sprintf( esc_html__( 'Filters active %s(%s)%s', 'nm-framework' ), '<span>', $filters_count, '</span>' )
);
}
// Search
if ( ! empty( $_REQUEST['s'] ) ) { // Is search query set and not empty?
$results_bar_class .= ' is-search';
$results_bar_buttons['search_taxonomy'] = array(
'id' => 'nm-shop-search-taxonomy-reset',
'title' => sprintf( esc_html__( 'Search results for %s“%s”%s', 'nm-framework' ), '<span>', esc_html( $_REQUEST['s'] ), '</span>' )
);
}
// Taxonomy
else if ( is_product_taxonomy() ) {
$results_bar_buttons['search_taxonomy'] = array(
'id' => 'nm-shop-search-taxonomy-reset'
);
$current_term = $GLOBALS['wp_query']->get_queried_object();
if ( is_product_category() ) {
$results_bar_class .= ' is-category';
$results_bar_buttons['search_taxonomy']['title'] = sprintf( esc_html__( 'Showing %s“%s”%s', 'nm-framework' ), '<span>', esc_html( $current_term->name ), '</span>' );
} else {
$results_bar_class .= ' is-tag';
$results_bar_buttons['search_taxonomy']['title'] = sprintf( esc_html__( 'Products tagged %s“%s”%s', 'nm-framework' ), '<span>', esc_html( $current_term->name ), '</span>' );
}
}
if ( ! empty( $results_bar_buttons ) ) :
?>
<div class="nm-shop-results-bar <?php echo esc_attr( $results_bar_class ); ?>">
<?php
$shop_url = esc_url( get_permalink( wc_get_page_id( 'shop' ) ) );
foreach ( $results_bar_buttons as $button ) {
printf( '<i class="nm-font nm-font-close2"></i>%s',
$nm_theme_options['shop_filters_enable_ajax'] ? '#' : $shop_url,
$button['id'],
$shop_url,
$button['title']
);
}
?>
</div>
<?php endif; ?>
I think that's the code part which directs the users to the shop page, but i don't know how to change it, so that it just reset the selected filters.
<div class="nm-shop-results-bar <?php echo esc_attr( $results_bar_class ); ?>">
<?php
$shop_url = esc_url( get_permalink( wc_get_page_id( 'shop' ) ) );
foreach ( $results_bar_buttons as $button ) {
printf( '<i class="nm-font nm-font-close2"></i>%s',
$nm_theme_options['shop_filters_enable_ajax'] ? '#' : $shop_url,
$button['id'],
$shop_url,
$button['title']
);
}
?>
</div>
<?php endif; ?>
Javascript
if (self.filtersEnableAjax) {
/* Bind: Results bar - Filters reset link */
self.$shopWrap.on('click', '#nm-shop-filters-reset', function(e) {
e.preventDefault();
var resetUrl = location.href.replace(location.search, ''); // Get current URL without query-strings
self.shopGetPage(resetUrl);
});
/* Bind: Results bar - Search/taxonomy reset link */
self.$shopWrap.on('click', '#nm-shop-search-taxonomy-reset', function(e) {
e.preventDefault();
var $resetButton = $(this);
if ($resetButton.closest('.nm-shop-results-bar').hasClass('is-search')) {
// Search
var urlSearchParam = self.urlGetParameter('s'), // Check for the "s" parameter in the current page URL
// Search from external page: Get default/main shop URL (current URL may not be the default shop URL)
// Search from shop page: Get current URL without query-strings (current URL is a shop URL)
resetUrl = (urlSearchParam) ? $resetButton.data('shop-url') : location.href.replace(location.search, '');
} else {
// Category or tag
var resetUrl = $resetButton.data('shop-url'); // Get default/main shop URL
}
self.shopGetPage(resetUrl);
});
}
}
I would like to add a meta box to a custom post type that works like the product gallery meta box that Woocommerce uses. I actually have Woocommerce installed on this project.
I found the code that Woocommerce uses to create the product gallery meta box, and I can get the meta box to display, but the jQuery is not there to pull up the media lightbox that Wordpress uses to add images. I don't know if there is a hook I need to add or if I should enqueue a script?
Here is what I have so far (based on the WooCommerce code but altered for my use)
<?php
function add_mtg_meta_boxes() {
add_meta_box('mtg_hotel_gallery_meta_box', 'Hotel Gallery', 'setup_mtg_hotel_gallery_meta_box', 'meeting', 'side', 'low');
}
add_action('add_meta_boxes', 'add_mtg_meta_boxes');
function setup_mtg_hotel_gallery_meta_box($post) {
echo '<input type="hidden" name="mtg_hotel_gallery_meta_box_nonce" value="'. wp_create_nonce('mtg_hotel_gallery_meta_box'). '" />';
?>
<div id="product_images_container">
<ul class="product_images">
<?php
if ( metadata_exists( 'post', $post->ID, '_product_image_gallery' ) ) {
$product_image_gallery = get_post_meta( $post->ID, '_product_image_gallery', true );
} else {
// Backwards compatibility.
$attachment_ids = get_posts( 'post_parent=' . $post->ID . '&numberposts=-1&post_type=attachment&orderby=menu_order&order=ASC&post_mime_type=image&fields=ids&meta_key=_woocommerce_exclude_image&meta_value=0' );
$attachment_ids = array_diff( $attachment_ids, array( get_post_thumbnail_id() ) );
$product_image_gallery = implode( ',', $attachment_ids );
}
$attachments = array_filter( explode( ',', $product_image_gallery ) );
$update_meta = false;
$updated_gallery_ids = array();
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $attachment_id ) {
$attachment = wp_get_attachment_image( $attachment_id, 'thumbnail' );
// if attachment is empty skip
if ( empty( $attachment ) ) {
$update_meta = true;
continue;
}
echo '<li class="image" data-attachment_id="' . esc_attr( $attachment_id ) . '">
' . $attachment . '
<ul class="actions">
<li>' . __( 'Delete', 'woocommerce' ) . '</li>
</ul>
</li>';
// rebuild ids to be saved
$updated_gallery_ids[] = $attachment_id;
}
// need to update product meta to set new gallery ids
if ( $update_meta ) {
update_post_meta( $post->ID, '_product_image_gallery', implode( ',', $updated_gallery_ids ) );
}
}
?>
</ul>
<input type="hidden" id="product_image_gallery" name="product_image_gallery" value="<?php echo esc_attr( $product_image_gallery ); ?>" />
</div>
<p class="add_product_images hide-if-no-js">
<?php _e( 'Add product gallery images', 'woocommerce' ); ?>
</p>
<?php
}
function save_mtg_hotel_gallery_meta_box($post_id) {
// check nonce
if (!isset($_POST['mtg_gallery_meta_box_nonce']) || !wp_verify_nonce($_POST['mtg_gallery_meta_box_nonce'], 'mtg_gallery_meta_box')) {
return $post_id;
}
// check capabilities
if ('meeting' == $_POST['post_type']) {
if (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
} elseif (!current_user_can('edit_page', $post_id)) {
return $post_id;
}
// exit on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
$attachment_ids = isset( $_POST['product_image_gallery'] ) ? array_filter( explode( ',', wc_clean( $_POST['product_image_gallery'] ) ) ) : array();
update_post_meta( $post_id, '_product_image_gallery', implode( ',', $attachment_ids ) );
}
add_action('save_post', 'save_mtg_hotel_gallery_meta_box');
?>
First To Enable the Wordpress Wp Media Uploader you have to enqueue the media js by using wp_enqueue_media(); inside you functions.php admin_enqueue_scripts hook,
function enqueue_media_uploader() {
wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts', 'enqueue_media_uploader' );
and then you have to assign an id to your anchor which clicked media.uploader
will pop up.Like this...
<?php _e( 'Add product gallery images', 'woocommerce' ); ?>
And then to bind the onclick listener to add_product_image id add this line of code to your custom js file...
jQuery('#add_product_image').click(function(e) {
e.preventDefault();
var image = wp.media({
title: 'Upload Image',
multiple: false
}).open()
.on('select', function(e){
var uploaded_image = image.state().get('selection').first();
var image_url = uploaded_image.toJSON().url;
var image_id = uploaded_image.toJSON().id;
});
});
Here image_url var is uploaded or selected image src url and image_id var is id of the image or attachment.
I'm building a Woocommerce site.
In the Shop grid overview, I'm showing the Featured Image of each product (see link). This Featured image will be cropped to maintain the same image ratio in the Shop Grid.
In the Single Product page, I managed to hide the Featured Image, and make the first of the thumbnails appear in big (see link).
I did so with the following code:
<div class="images">
<?php
$imgid = $product->get_gallery_attachment_ids();
?>
<a href="<?php echo wp_get_attachment_url( $imgid[0] ); ?>"
class="woocommerce-main-image zoom first"
rel="lightbox[product-gallery]">
<img src="<?php echo wp_get_attachment_url( $imgid[0] ); ?>" alt="">
</a>
<?php do_action( 'woocommerce_product_thumbnails' ); ?>
</div>
<script>
jQuery('.thumbnails.columns-3 a:first-child').hide()
</script>
The first part will find the first image in the gallery array and show it in big size (class woocommerce-main-image zoom first) while linking to the lightbox.
Then I call the thumbnails, and I hide the first one using jQuery to avoid a duplicate (first big size image and first thumb are the same).
The problem now is that in the Lightbox, the first image will appear duplicated, as it exists two times in the array, the first one I call in big, and the one from the thumbs array.
Any tips on how to not show the image twice in the lightbox?
Someone mentioned that I should filter the following function, but as of now I don't know how to do that.
public function get_gallery_attachment_ids() {
return apply_filters( 'woocommerce_product_gallery_attachment_ids', array_filter( (array) explode( ',', $this->product_image_gallery ) ), $this );
}
I think that using Multiple Post Thumbnails is the easiest solution. It is exactly for displaying different featured images in different locations.
Option #1: Multiple Post Thumbnails
You would install the plugin and then add the following to your theme's functions.php. It isn't 100% tested so there may be a stray typo or something. Full documentation is here.
// register the new thumbnail
function so_31835142_register_extra_thumbnail() {
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => __('Product Loop Image', 'your-theme'),
'id' => 'product-loop-image',
'post_type' => 'product'
)
);
}
}
add_action( 'after_setup_theme', 'so_31835142_register_extra_thumbnail' );
// remove the existing loop thumbnail
function so_31835142_swap_loop_product_thumbnail(){
if (class_exists('MultiPostThumbnails')) {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'so_31835142_loop_product_thumbnail', 10 );
}
}
add_action( 'woocommerce_before_shop_loop_item, 'so_31835142_swap_loop_product_thumbnail' );
// Display the Secondary Thumbnail
function so_31835142_loop_product_thumbnail(){
global $product;
$thumbnail = MultiPostThumbnails::get_post_thumbnail(
'product',
'product-loop-image',
$product->id,
'shop_catalog'
);
if ( $thumbnail ) {
return $thumbnail;
} elseif ( wc_placeholder_img_src() ) {
return wc_placeholder_img( $size );
}
}
Then to use it, you'd set "Product Loop Image" the same way you traditionally set the "featured image". And this new image would be used in the loop.
Option #2: Template Overrides
But as an alternative, if you insist, you can write a custom single-product/product-image.php template and put it in your theme's woocommerce templates folder.
In this alternative we will only show images from the image gallery on the single product page, $product->get_gallery_attachment_ids(), and we will use a basic PHP loop and counter system to display the images differently depending on where they are in the loop. IE. the first image will display as the post thumbnail used to display and the remaining items will display as thumbs.
This section I have tested, so it should (in theory) be good to go.
<?php
/**
* Single Product Image
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.0.14
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post, $woocommerce, $product;
?>
<div class="images">
<?php
$attachment_ids = $product->get_gallery_attachment_ids();
if ( $attachment_ids ) {
$loop = 0;
$columns = apply_filters( 'woocommerce_product_thumbnails_columns', 3 );
$attachment_count = count( $attachment_ids );
foreach ( $attachment_ids as $attachment_id ) {
// here's your first image
if( $loop === 0 ){
$image_title = esc_attr( get_the_title( $attachment_id ) );
$image_caption = get_post( $attachment_id )->post_excerpt;
$image_link = wp_get_attachment_url( $attachment_id );
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_large_thumbnail_size', 'shop_single' ), null, array(
'title' => $image_title,
'alt' => $image_title
) );
if ( $attachment_count > 0 ) {
$gallery = '[product-gallery]';
} else {
$gallery = '';
}
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '%s', $image_link, $image_caption, $image ), $post->ID );
// resume the thumbnails for the rest
} else {
// open the thumbnails div
if( $loop === 1 ) { ?>
<div class="thumbnails <?php echo 'columns-' . $columns; ?>">
<?php }
$classes = array( 'zoom' );
if ( $loop == 0 || $loop % $columns == 0 )
$classes[] = 'first';
if ( ( $loop + 1 ) % $columns == 0 )
$classes[] = 'last';
$image_link = wp_get_attachment_url( $attachment_id );
if ( ! $image_link )
continue;
$image_title = esc_attr( get_the_title( $attachment_id ) );
$image_caption = esc_attr( get_post_field( 'post_excerpt', $attachment_id ) );
$image = wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ), 0, $attr = array(
'title' => $image_title,
'alt' => $image_title
) );
$image_class = esc_attr( implode( ' ', $classes ) );
echo apply_filters( 'woocommerce_single_product_image_thumbnail_html', sprintf( '%s', $image_link, $image_class, $image_caption, $image ), $attachment_id, $post->ID, $image_class );
// close the thumbnails div
if( $loop === $attachment_count ) { ?>
</div>
<?php }
}
$loop++;
}
?>
<?php
} else {
echo apply_filters( 'woocommerce_single_product_image_html', sprintf( '<img src="%s" alt="%s" />', wc_placeholder_img_src(), __( 'Placeholder', 'woocommerce' ) ), $post->ID );
}
?>
</div>
It is an external group blogs plugin
It gives group creators and administrators on your BuddyPress install the ability to attach external blog RSS feeds to groups.
Blog posts will appear within the activity stream for the group.
New posts will automatically be pulled every hour, or every 30 minutes if someone specifically visits a group page.
There are so many bugs that I found.
1) it is not fetching feeds automatically
2) if I try to manually updates feeds it is reposting the same entries, I mean it is not fetching new feeds.
3) WordPress admin bar is also not working properly with this plugin.
This plugin contains 2 webpages.
First and the main page is loader.php
<?php
/*
Plugin Name: External Group Blogs
Plugin URI: http://wordpress.org/extend/plugins/external-group-blogs/
Description: Allow group creators to supply external blog RSS feeds that will attach future posts on blogs to a group.
*/
/* Only load the plugin functions if BuddyPress is loaded and initialized. */
function bp_groupblogs_init() {
require( dirname( __FILE__ ) . '/bp-groups-externalblogs.php' );
}
add_action( 'bp_init', 'bp_groupblogs_init' );
/* On activation register the cron to refresh external blog posts. */
function bp_groupblogs_activate() {
wp_schedule_event( time(), 'hourly', 'bp_groupblogs_cron' );
}
register_activation_hook( __FILE__, 'bp_groupblogs_activate' );
/* On deacativation, clear the cron. */
function bp_groupblogs_deactivate() {
wp_clear_scheduled_hook( 'bp_groupblogs_cron' );
/* Remove all external blog activity */
if ( function_exists( 'bp_activity_delete' ) )
bp_activity_delete( array( 'type' => 'exb' ) );
}
register_deactivation_hook( __FILE__, 'bp_groupblogs_deactivate' );
?>
And the 2nd file is bp-groups-externalblogs.php
<?php
/* Group blog extension using the BuddyPress group extension API */
if ( class_exists('BP_Group_Extension' ) ) {
class Group_External_Blogs extends BP_Group_Extension {
function __construct() {
global $bp;
$this->name = __( 'External Blogs', 'bp-groups-externalblogs' );
$this->slug = 'external-blog-feeds';
$this->create_step_position = 21;
$this->enable_nav_item = false;
}
function create_screen() {
global $bp;
if ( !bp_is_group_creation_step( $this->slug ) )
return false;
?>
<p><?php _e(
"Add RSS feeds of blogs you'd like to attach to this group in the box below.
Any future posts on these blogs will show up on the group page and be recorded
in activity streams.", 'bp-groups-externalblogs' ) ?>
</p>
<p class="desc"><?php _e( "Seperate URL's with commas.", 'bp-groups-externalblogs' ) ?></span>
<p>
<label for="blogfeeds"><?php _e( "Feed URL's:", 'bp-groups-externalblogs' ) ?></label>
<textarea name="blogfeeds" id="blogfeeds"><?php echo attribute_escape( implode( ', ', (array)groups_get_groupmeta( $bp->groups->current_group->id, 'blogfeeds' ) ) ) ?></textarea>
</p>
<?php
wp_nonce_field( 'groups_create_save_' . $this->slug );
}
function create_screen_save() {
global $bp;
check_admin_referer( 'groups_create_save_' . $this->slug );
$unfiltered_feeds = explode( ',', $_POST['blogfeeds'] );
foreach( (array) $unfiltered_feeds as $blog_feed ) {
if ( !empty( $blog_feed ) )
$blog_feeds[] = trim( $blog_feed );
}
groups_update_groupmeta( $bp->groups->current_group->id, 'blogfeeds', $blog_feeds );
groups_update_groupmeta( $bp->groups->current_group->id, 'bp_groupblogs_lastupdate', gmdate( "Y-m-d H:i:s" ) );
/* Fetch */
bp_groupblogs_fetch_group_feeds( $bp->groups->current_group->id );
}
function edit_screen() {
global $bp;
if ( !bp_is_group_admin_screen( $this->slug ) )
return false; ?>
<p class="desc"><?php _e( "Enter RSS feed URL's for blogs you would like to attach to this group. Any future posts on these blogs will show on the group activity stream. Seperate URL's with commas.", 'bp-groups-externalblogs' ) ?></span>
<p>
<label for="blogfeeds"><?php _e( "Feed URL's:", 'bp-groups-externalblogs' ) ?></label>
<textarea name="blogfeeds" id="blogfeeds"><?php echo attribute_escape( implode( ', ', (array)groups_get_groupmeta( $bp->groups->current_group->id, 'blogfeeds' ) ) ) ?></textarea>
</p>
<input type="submit" name="save" value="<?php _e( "Update Feed URL's", 'bp-groups-externalblogs' ) ?>" />
<?php
wp_nonce_field( 'groups_edit_save_' . $this->slug );
}
function edit_screen_save() {
global $bp;
if ( !isset( $_POST['save'] ) )
return false;
check_admin_referer( 'groups_edit_save_' . $this->slug );
$existing_feeds = (array)groups_get_groupmeta( $bp->groups->current_group->id, 'blogfeeds' );
$unfiltered_feeds = explode( ',', $_POST['blogfeeds'] );
foreach( (array) $unfiltered_feeds as $blog_feed ) {
if ( !empty( $blog_feed ) )
$blog_feeds[] = trim( $blog_feed );
}
/* Loop and find any feeds that have been removed, so we can delete activity stream items */
if ( !empty( $existing_feeds ) ) {
foreach( (array) $existing_feeds as $feed ) {
if ( !in_array( $feed, (array) $blog_feeds ) )
$removed[] = $feed;
}
}
if ( $removed ) {
/* Remove activity stream items for this feed */
include_once( ABSPATH . WPINC . '/rss.php' );
foreach( (array) $removed as $feed ) {
$rss = fetch_rss( trim( $feed ) );
if ( function_exists( 'bp_activity_delete' ) ) {
bp_activity_delete( array(
'item_id' => $bp->groups->current_group->id,
'secondary_item_id' => wp_hash( $rss->channel['link'] ),
'component' => $bp->groups->id,
'type' => 'exb'
) );
}
}
}
groups_update_groupmeta( $bp->groups->current_group->id, 'blogfeeds', $blog_feeds );
groups_update_groupmeta( $bp->groups->current_group->id, 'bp_groupblogs_lastupdate', gmdate( "Y-m-d H:i:s" ) );
/* Re-fetch */
bp_groupblogs_fetch_group_feeds( $bp->groups->current_group->id );
bp_core_add_message( __( 'External blog feeds updated successfully!', 'bp-groups-externalblogs' ) );
bp_core_redirect( bp_get_group_permalink( $bp->groups->current_group ) . '/admin/' . $this->slug );
}
/* We don't need display functions since the group activity stream handles it all. */
function display() {}
function widget_display() {}
}
bp_register_group_extension( 'Group_External_Blogs' );
function bp_groupblogs_fetch_group_feeds( $group_id = false ) {
global $bp;
include_once( ABSPATH . 'wp-includes/rss.php' );
if ( empty( $group_id ) )
$group_id = $bp->groups->current_group->id;
if ( $group_id == $bp->groups->current_group->id )
$group = $bp->groups->current_group;
else
$group = new BP_Groups_Group( $group_id );
if ( !$group )
return false;
$group_blogs = groups_get_groupmeta( $group_id, 'blogfeeds' );
$group_blogs = explode(";",$group_blogs[0]);
/* Set the visibility */
$hide_sitewide = ( 'public' != $group->status ) ? true : false;
foreach ( (array) $group_blogs as $feed_url ) {
$rss = fetch_feed( trim( $feed_url ) );
if (!is_wp_error($rss) ) {
foreach ( $rss->get_items(0,10) as $item ) {;
$key = $item->get_date( 'U' );
$items[$key]['title'] = $item->get_title();
$items[$key]['subtitle'] = $item->get_title();
//$items[$key]['author'] = $item->get_author()->get_name();
$items[$key]['blogname'] = $item->get_feed()->get_title();
$items[$key]['link'] = $item->get_permalink();
$items[$key]['blogurl'] = $item->get_feed()->get_link();
$items[$key]['description'] = $item->get_description();
$items[$key]['source'] = $item->get_source();
$items[$key]['copyright'] = $item->get_copyright();
}
}
}
if ( $items ) {
ksort($items);
$items = array_reverse($items, true);
} else {
return false;
}
/* Record found blog posts in activity streams */
foreach ( (array) $items as $post_date => $post ) {
//var_dump($post);
if (substr($post['blogname'],0,7) == "Twitter") {
$activity_action = sprintf( __( '%s from %s in the group %s', 'bp-groups-externalblogs' ), '<a class="feed-link" href="' . esc_attr( $post['link'] ) . '">Tweet</a>', '<a class="feed-author" href="' . esc_attr( $post['blogurl'] ) . '">' . attribute_escape( $post['blogname'] ) . '</a>', '' . attribute_escape( $group->name ) . '' );
} else {
$activity_action = sprintf( __( 'Blog: %s from %s in the group %s', 'bp-groups-externalblogs' ), '<a class="feed-link" href="' . esc_attr( $post['link'] ) . '">' . esc_attr( $post['title'] ) . '</a>', '<a class="feed-author" href="' . esc_attr( $post['blogurl'] ) . '">' . attribute_escape( $post['blogname'] ) . '</a>', '' . attribute_escape( $group->name ) . '' );
}
$activity_content = '<div>' . strip_tags( bp_create_excerpt( $post['description'], 175 ) ) . '</div>';
$activity_content = apply_filters( 'bp_groupblogs_activity_content', $activity_content, $post, $group );
/* Fetch an existing activity_id if one exists. */
if ( function_exists( 'bp_activity_get_activity_id' ) )
$id = bp_activity_get_activity_id( array( 'user_id' => false, 'action' => $activity_action, 'component' => $bp->groups->id, 'type' => 'exb', 'item_id' => $group_id, 'secondary_item_id' => wp_hash( $post['blogurl'] ) ) );
/* Record or update in activity streams. */
groups_record_activity( array(
'id' => $id,
'user_id' => false,
'action' => $activity_action,
'content' => $activity_content,
'primary_link' => $item->get_link(),
'type' => 'exb',
'item_id' => $group_id,
'secondary_item_id' => wp_hash( $post['blogurl'] ),
'recorded_time' => gmdate( "Y-m-d H:i:s", $post_date ),
'hide_sitewide' => $hide_sitewide
) );
}
return $items;
}
/* Add a filter option to the filter select box on group activity pages */
function bp_groupblogs_add_filter() { ?>
<option value="exb"><?php _e( 'External Blogs', 'bp-groups-externalblogs' ) ?></option><?php
}
add_action( 'bp_group_activity_filter_options', 'bp_groupblogs_add_filter' );
add_action( 'bp_activity_filter_options', 'bp_groupblogs_add_filter' );
/* Add a filter option groups avatar */
/* Fetch group twitter posts after 30 mins expires and someone hits the group page */
function bp_groupblogs_refetch() {
global $bp;
$last_refetch = groups_get_groupmeta( $bp->groups->current_group->id, 'bp_groupblogs_lastupdate' );
if ( strtotime( gmdate( "Y-m-d H:i:s" ) ) >= strtotime( '+30 minutes', strtotime( $last_refetch ) ) )
add_action( 'wp_footer', 'bp_groupblogs_refetch' );
/* Refetch the latest group twitter posts via AJAX so we don't stall a page load. */
function _bp_groupblogs_refetch() {
global $bp; ?>
<script type="text/javascript">
jQuery(document).ready( function() {
jQuery.post( ajaxurl, {
action: 'refetch_groupblogs',
'cookie': encodeURIComponent(document.cookie),
'group_id': <?php echo $bp->groups->current_group->id ?>
});
});
</script><?php
groups_update_groupmeta( $bp->groups->current_group->id, 'bp_groupblogs_lastupdate', gmdate( "Y-m-d H:i:s" ) );
}
}
add_action( 'groups_screen_group_home', 'bp_groupblogs_refetch' );
/* Refresh via an AJAX post for the group */
function bp_groupblogs_ajax_refresh() {
bp_groupblogs_fetch_group_feeds( $_POST['group_id'] );
}
add_action( 'wp_ajax_refetch_groupblogs', 'bp_groupblogs_ajax_refresh' );
function bp_groupblogs_cron_refresh() {
global $bp, $wpdb;
$group_ids = $wpdb->get_col( $wpdb->prepare( "SELECT group_id FROM " . $bp->groups->table_name_groupmeta . " WHERE meta_key = 'blogfeeds'" ) );
foreach( $group_ids as $group_id )
bp_groupblogs_fetch_group_feeds( $group_id );
}
add_action( 'bp_groupblogs_cron', 'bp_groupblogs_cron_refresh' );
}
// Add a filter option groups avatar
function bp_groupblogs_avatar_type($var) {
global $activities_template, $bp;
if ( $activities_template->activity->type == "exb" ) {
return 'group';
} else {
return $var;
}
}
add_action( 'bp_get_activity_avatar_object_groups', 'bp_groupblogs_avatar_type');
add_action( 'bp_get_activity_avatar_object_activity', 'bp_groupblogs_avatar_type');
function bp_groupblogs_avatar_id($var) {
global $activities_template, $bp;
if ( $activities_template->activity->type == "exb" ) {
return $activities_template->activity->item_id;
}
return $var;
}
add_action( 'bp_get_activity_avatar_item_id', 'bp_groupblogs_avatar_id');
?>
I have a suggestion for stopping data repeating in activity stream. Maybe use wp-cron api and a current date xml feeds fetching system once in a day. So it will not repeat the same feeds in a group activity stream. We need a criteria by which we can stop data repetition in the bp group activity stream. It is a part of my project. Is there another way of fetching feeds and saving it to the mysql table (in the group activity stream) and then show it as a latest group updates?
Actually IT IS fetching automatically.
I see the cron usage in a plugin. WordPress Cron fires only when any user opens your site. So no users (no pageviews) - no cron activity. On every pageview WP-Cron check whether the time to fire any function came or not.
Perhaps this plugin will help you a bit - display a RSS feed in a separate group tab.
I have this question on the Wordpress stack exchange as well, but not having any luck there. So, as my solution probably involves me hard-coding php and css, It may be better to have it here.
I'm using 'Flex Slider' plugin - that works on top of 'WP rotator' plug-in on my Wordpress 3.2 website. I have it implemented fine, and beginning to look at inserting my content - but I need to add a caption to be on top of the slider. As are present on most sliders on the web, within the documentation of the non-Wordpress plugin of the tool it suggests I can do something like;
<div class="flex-container">
<div class="flexslider">
<ul class="slides">
<li>
<img src="slide1.jpg" />
<p class="flex-caption">Captions and cupcakes. Winning combination.</p>
</li>
<li>
<img src="slide2.jpg" />
<p class="flex-caption">This image is wrapped in a link!</p>
</li>
<li>
<img src="slide3.jpg" />
</li>
</ul>
</div>
</div>
Problem is; with the Wordpress plug-in version, I can't find that markup to work inside.
Here's the only non-css non-js file in the plug-ins directory, so I assume I have to work in there.
I've tried inserting the mark-up that was suggested non-Wordpress above, but not sure where to insert it as it's broke it with my attempts thus far.
<?php
/*
Plugin Name: Flex Slider for WP Rotator
Plugin URI: http://wordpress.org/extend/plugins/flex-slider-for-wp-rotator/
Description: Turns WP Rotator into FlexSlider, a fully responsive jQuery slider.
Version: 1.1
Author: Bill Erickson
Author URI: http://www.billerickson.net/blog/wordpress-guide
*/
class BE_Flex_Slider {
var $instance;
function __construct() {
$this->instance =& $this;
register_activation_hook( __FILE__, array( $this, 'activation_hook' ) );
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
/**
* Activation Hook
* Confirm WP Rotator is currently active
*/
function activation_hook() {
if( !function_exists( 'wp_rotator_option' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( sprintf( __( 'Sorry, you can’t activate unless you have installed WP Rotator', 'flex-slider-for-wp-rotator'), 'http://wordpress.org/extend/plugins/wp-rotator/' ) );
}
}
function init() {
// Remove original scripts and styles
remove_action('wp_head','wp_rotator_css');
remove_action('admin_head','wp_rotator_css');
remove_action('wp_head','wp_rotator_javascript');
remove_action('admin_head','wp_rotator_javascript');
remove_action('init','wp_rotator_add_jquery');
remove_action('admin_init','wp_rotator_add_jquery');
// Enqueue Scripts and Styles
add_action( 'init', array( $this, 'enqueue_scripts_and_styles' ) );
// Remove original outer markup
remove_action( 'wp_rotator', 'wp_rotator' );
// Add new markup
add_action( 'wp_rotator', array( $this, 'flex_slider' ) );
remove_shortcode( 'wp_rotator' );
add_shortcode( 'wp_rotator', array( $this, 'flex_slider_markup' ) );
}
function enqueue_scripts_and_styles() {
// Use this filter to limit where the scripts are enqueued.
$show = apply_filters( 'be_flex_slider_show_scripts', true );
if ( true === $show ) {
wp_enqueue_style( 'flex-slider', plugins_url( 'flexslider.css', __FILE__ ) );
wp_enqueue_script( 'jquery ');
wp_enqueue_script( 'flex-slider', plugins_url( 'jquery.flexslider-min.js', __FILE__ ), array( 'jquery' ) );
add_action( 'wp_head', array( $this, 'flex_slider_settings' ) );
}
}
function flex_slider_settings() {
?>
<script type="text/javascript" charset="utf-8">
jQuery(window).load(function() {
jQuery('.flexslider').flexslider({
<?php
$flex_settings = array(
'animation' => '"' . wp_rotator_option( 'animate_style' ) . '"',
'slideshowSpeed' => wp_rotator_option( 'rest_ms' ),
'animationDuration' => wp_rotator_option( 'animate_ms' ),
);
$flex_slide_settings = array(
'controlsContainer' => '".flex-container"'
);
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$flex_settings = array_merge( $flex_settings, $flex_slide_settings );
$flex_settings = apply_filters( 'be_flex_slider_settings', $flex_settings );
foreach ( $flex_settings as $field => $value ) {
echo $field . ': ' . $value . ', ';
}
?>
});
});
</script>
<?php
}
function flex_slider_markup() {
$output = '';
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$output .= '<div class="flex-container">';
$output .= '<div class="flexslider"><ul class="slides">';
$loop = new WP_Query( esc_attr( wp_rotator_option('query_vars') ) );
while ( $loop->have_posts() ): $loop->the_post(); global $post;
$url = esc_url ( get_post_meta( $post->ID, 'wp_rotator_url', true ) );
if ( empty( $url ) ) $url = get_permalink($post->ID);
$show_info = esc_attr( get_post_meta( $post->ID, 'wp_rotator_show_info', true ) );
if ( true == $show_info ) {
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
} else {
$info = '';
}
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'wp_rotator' );
$slide = '<li><img src="' . $image[0] . '" />' . $info . '</li>';
$output .= apply_filters( 'be_flex_slider_slide', $slide );
endwhile; wp_reset_query();
$output .= '</ul></div>';
if( 'slide' == wp_rotator_option( 'animate_style' ) )
$output .= '</div>';
return $output;
}
function flex_slider() {
echo $this->flex_slider_markup();
}
}
new BE_Flex_Slider;
?>
I have contacted the plug-in developer, he's not responding so I assume hes not going to support my question - so I'm left to handcode.
http://wordpress.org/extend/plugins/wp-rotator/
http://flex.madebymufffin.com/
http://wordpress.org/extend/plugins/flex-slider-for-wp-rotator/
Thanks for any pointers!
It looks like captions are automatically added to the slider as long as you set the post to show rotator info (wp_rotator_show_info... probably on the plugin settings page or on your individual post page). The automatic caption is made up of the title of the post plus the excerpt. Here's the key part in the plugin above:
$show_info = esc_attr( get_post_meta( $post->ID, 'wp_rotator_show_info', true ) );
if ( true == $show_info ) {
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
} else {
$info = '';
}
UPDATE: If you want the caption to show no matter what, replace the above portion with this:
$title = get_the_title();
if ( get_the_excerpt() ) $excerpt = get_the_excerpt();
else $excerpt = '';
$caption = $title . ' <span class="excerpt">' . $excerpt . '</span>';
$info = '<p class="flex-caption">' . apply_filters( 'be_flex_slider_caption', $caption, $title, $excerpt ) . '</p>';
Note that I merely deleted the part that checks for wp_rotator_show_info.