I wanted to pull the settings found for Media settings (thumbnail, medium and large declared sizes) into my template. How do I do it, I cannot seem to find on Wordpress API, or maybe I didn't search quite well.
Thanks
There does not appear to be a special set of functions to retrieve these, but they are saved in the options table, so with a little bit of effort we can retrieve all those values.
Image Sizes
For the image dimension settings, you should be able to get these from the options table. The default sizes are 'medium', 'thumbnail', 'large', and 'post-thumbnail'. Keep in mind that crop only exists for thumbnails, and that it is a checkbox.
To get dimensions do the following:
$thumbnail_size = array (
'width' = get_option('thumbnail_size_w'),
'height'= get_option('thumbnail_size_h'),
'crop' = get_option('thumbnail_crop')
);
$medium_size = array (
'width' = get_option('medium_size_w'),
'height'= get_option('medium_size_h')
);
$large_size = array (
'width' = get_option('large_size_w'),
'height'= get_option('large_size_h')
);
Embeds
Keep im mind that auto refers to whether or not to use oEmbed to auto embed media when people paste in a url.
$embeds = array(
'auto' => get_option('embed_autourls'),
'width'=> get_option('embed_size_w'),
'height' => get_option('embed_size_h')
);
Uploading Files
The upload path is relative to ABSPATH (that is to say, it can not be outside the WordPress root directory - there is also a constant 'UPLOADS' that can change this setting. Keep in mind that the Year/Month option is another checkbox.
$upload_path = array(
'path' => get_option('upload_path'),
'url_path' => get_option('upload_url_path'),
'yearmonth_folders' => get_option('uploads_use_yearmonth_folders')
);
If we wanted to put this together as one big function for easy use...
/**
* Returns settings from the Media Settings page.
*
* #author Eddie Moya
* #param string $option (Optional) The key for the particular set of options wanted.
*
* #return array Returns a set of specific options if $options is set, otherwise returns all optons.
*/
function get_media_settings($option = null){
$thumbnail_size = array (
'width' = get_option('thumbnail_size_w'),
'height'= get_option('thumbnail_size_h'),
'crop' = get_option('thumbnail_crop')
);
$medium_size = array (
'width' = get_option('medium_size_w'),
'height'= get_option('medium_size_h')
);
$large_size = array (
'width' = get_option('large_size_w'),
'height'= get_option('large_size_h')
);
$embeds = array(
'auto' => get_option('embed_autourls'),
'width'=> get_option('embed_size_w'),
'height' => get_option('embed_size_h')
);
$upload_path = array(
'path' => get_option('upload_path'),
'url_path' => get_option('upload_url_path'),
'yearmonth_folders' => get_option('uploads_use_yearmonth_folders')
);
$settings = array(
'thumbnail_size' => $thumbnail_size,
'medium_size' => $medium_size,
'large_size' => $large_size,
'embed_size' => $embeds,
'upload_path' => $upload_path
);
if(!empty($option)){
return $settings[$option];
else
return $settings;
}
You are of course free or organize all that data however you want, this is just an example. You can obviously also just directly get the options you want, although it might be annoying to have to remember all their names.
Related
using WooCommerce I put this snippet in function.php to define the size of images overwriting the panel settings :
function yourtheme_woocommerce_image_dimensions() {
$single = array(
'width' => '400', // px
'height' => '300', // px
'crop' => 1 // true
);
$thumbnail = array(
'width' => '180', // px
'height' => '135', // px
'crop' => 1 // false
);
$catalog = array(
'width' => '140', // px
'height' => '105', // px
'crop' => 1 // true
);
// Image sizes
update_option( 'shop_single_image_size', $single ); // Single product image
update_option( 'shop_thumbnail_image_size', $thumbnail );
update_option( 'shop_catalog_image_size', $catalog ); // Product category thumbs
All works fine but now I'd like to set different image sizes relating to $catalog just in single product page (where there's a widget showing that images).
I've tried with conditional statements, cause I don't want to use css tricks, but without luck.
Any help will be appreciate.
Thank you.
Did you re-generate the thumbnails after changing the size? If no, than try force regenerate thumbnails plugin to re-generate images that will have new sizes. When indicating new size in Wp functions, than it will affect new uploads but you also need to regenerate new sizes for previously uploaded ones.
Something appears to have changed in the new version of WooCommerce, this snippet to change the order of the items in a formatted address used to work fine....
add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address' );
/**
* woo_custom_order_formatted_billing_address
*
* #access public
* #since 1.0
* #return void
*/
function woo_custom_order_formatted_billing_address() {
$address = array(
'first_name' => $this->billing_first_name,
'last_name' => $this->billing_last_name,
'company' => $this->billing_company,
'address_1' => $this->billing_address_1,
'address_2' => $this->billing_address_2,
'city' => $this->billing_city,
'state' => $this->billing_state,
'postcode' => $this->billing_postcode,
'country' => $this->billing_country
);
return $address;
}
But now it returns the following error...
Fatal error: Using $this when not in object context in
Can anyone point me in the right direction of either what is going wrong or an alternative way of achieving it?
I'm seeing this example on the internet, but I don't know if it ever worked. First of because in 'woocommerce_order_formatted_billing_address' filter, that is applied in class-wc-order.php file, two arguments are provided, $address ARRAY, and a reference to current WC_Order OBJECT. But your definition of the filter function does not provide any arguments. Second, error that you receive describes the problem very accurately, pseudo-variable $this is available from within an object context, but your global function is not part of any object.
Enough with technicalities, the definition should look like this:
add_filter( 'woocommerce_order_formatted_billing_address' , 'woo_custom_order_formatted_billing_address', 10, 2 );
function woo_custom_order_formatted_billing_address( $address, $wc_order ) {
// make the changes to $address array here
// use for example, $wc_order->billing_first_name, instead of $this->billing_first_name
return $address;
}
#Eolis' answer doens't work for me, because Woocommerce applies WC()->countries->get_formatted_address( $address ) and this functions erases my new fields added to array on add_filter, so my solution is add field to previous field (first_name):
add_filter('woocommerce_order_formatted_billing_address', 'my_order_formatted_billing_address', 10, 2);
function my_order_formatted_billing_address($address, $wc_order) {
$billing_last_name_2 = get_post_meta( $wc_order->id, '_billing_last_name_2', true );
$address = array(
'postcode' => $wc_order->billing_postcode,
'last_name' => $wc_order->billing_last_name,
//\n force break line
'first_name' => $wc_order->billing_first_name . "\n" . $billing_last_name_2,
'address_1' => $wc_order->billing_address_1,
'address_2' => $wc_order->billing_address_2,
'city' => $wc_order->billing_city,
'state' => $wc_order->billing_state,
'country' => $wc_order->billing_country
);
return $address;
}
I have been reading through the Wordpress Source, trying to get a better understanding of how dynamic sidebars are rendered.
However, I am hitting a sticking point...
894 | do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );
I can't find where add_action('dynamic_sidebar', ... ) is defined. Without that part, I am sort of lost in what happens.
See the code here:
https://github.com/WordPress/WordPress/blob/b7c13e27c255e1fc1f03ab2ab432f1652a0ac212/wp-includes/widgets.php#L894
And to give more context, I am trying to figure out how to grab an array of widgets from a specific sidebar, and from there, I need to know how would you render each widget within that array.
I need finer control than dynamic_sidebar(...); gives me
Well, that specific line permits you to access each registered Widget properties, and it's used like:
<?php
/* Plugin Name: Test registered widgets */
add_action( 'dynamic_sidebar', 'sidebar_widgets_so_18666065' );
/**
* As this is an action hook, we don't return nothing
* use the passed values to do your stuff
*/
function sidebar_widgets_so_18666065( $registered_widget )
{
# Each registered widget passes the following array
/*
$registered_widget = Array
(
[name] => Meta
[id] => meta-2
[callback] => Array
(
[0] => WP_Widget_Meta Object
(
[id_base] => meta
[name] => Meta
[widget_options] => Array
(
[classname] => widget_meta
[description] => Log in/out, admin, feed and WordPress links
)
[control_options] => Array
(
[id_base] => meta
)
[number] => 2
[id] => meta-2
[updated] =>
[option_name] => widget_meta
)
[1] => display_callback
)
[params] => Array
(
[0] => Array
(
[number] => 2
)
)
[classname] => widget_meta
[description] => Log in/out, admin, feed and WordPress links
)
*/
}
Relevant search query at WordPress Answers.
I'm developing a social network with Buddypress, I created a RSS plugin to pull the RSS feed from the specified websites.
Everything is working, except when the RSS is posted to the activity stream. When I create the activity content to print a link, I set the link target to "_new" to open it in a new page.
Here's the code:
function wprss_add_to_activity_feed($item, $inserted_ID) {
$permalink = $item->get_permalink();
$title = $item->get_title();
$admin = get_user_by('login', 'admin');
# Generates the link
$activity_action = sprintf( __( '%s published a new RSS link: %s - ', 'buddypress'), bp_core_get_userlink( $admin->ID ), '' . attribute_escape( wprss_limit_rss_title_chars($title) ) . '');
/* Record this in activity streams */
bp_activity_add( array(
'user_id' => $admin->ID,
'item_id' => $inserted_ID,
'action' => $activity_action,
'component' => 'rss',
'primary_link' => $permalink,
'type' => 'activity_update',
'hide_sitewide' => false
));
}
It should come up with something like that:
Test
But it prints like that:
Test
Why is this happening?
The 'target' attribute is probably getting stripped by BuddyPress's implementation of the kses filters. You can whitelist the attribute as follows:
function se16329156_whitelist_target_in_activity_action( $allowedtags ) {
$allowedtags['a']['target'] = array();
return $allowedtags;
}
add_filter( 'bp_activity_allowed_tags', 'se16329156_whitelist_target_in_activity_action' );
This probably won't retroactively fix the issue for existing activity items - it's likely that they had the offending attribute stripped before being stored in the database. But it should help for future items.
What I am trying to do is pre-populate the sidebar widget area with some default widgets on theme activation.
if ( ! dynamic_sidebar( 'sidebar' ) ) :
does add the widgets but it doesnot show up in the sidebar of widgets section and
if ( is_active_sidebar( 'sidebar' ) ) {
this function doesnot work if the widgets are not loaded in the sidebar widgetized area.
I know it is possible but I am just out of idea. I googled but didnot find any solutions. Thank you for any help in advance.
It isn't clear from your answer if you use the after_switch_theme hook but that the moment you need to set the widgets.
To activate the widgets I suggest writing it directly into the database with get_option('sidebars_widgets') which should give an array, and save it with update_option('sidebars_widgets', $new_activated_widgets).
This should help you get started.
/**
* set new widgets on theme activate
* #param string $old_theme
* #param WP_Theme $WP_theme
*/
function set_default_theme_widgets ($old_theme, $WP_theme = null) {
// check if the new theme is your theme
// figure it out
var_dump($WP_theme);
// the name is (probably) the slug/id
$new_active_widgets = array (
'sidebar-name' => array (
'widget-name-1',
'widget-name-2',
'widget-name-3',
),
'footer-sidebar' => array(
'widget-name-1',
'widget-name-2',
'widget-name-3',
)
);
// save new widgets to DB
update_option('sidebars_widgets', $new_active_widgets);
}
add_action('after_switch_theme', 'set_default_theme_widgets', 10, 2);
Tested, just paste it in functions.php of your theme.
If anyone else needed to know how to add multiple default widgets (different instances) to multiple sidebars at the same time, the following code will add the widgets both to the page and under the admin widget tab. I realize that this may have been obvious to everyone but me.
So based on janw and kcssm's hard work:
function add_theme_widgets($old_theme, $WP_theme = null) {
$activate = array(
'right-sidebar' => array(
'recent-posts-1',
'categories-1',
'archives-1'
),
'footer-sidebar' => array(
'recent-posts-2',
'categories-2',
'archives-2'
)
);
/* the default titles will appear */
update_option('widget_recent-posts', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('widget_categories', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('widget_archives', array(
1 => array('title' => ''),
2 => array('title' => '')));
update_option('sidebars_widgets', $activate);
}
add_action('after_switch_theme', 'add_theme_widgets', 10, 2);
This will however delete any other settings, so tread carefully!