changing the wordpress gallery image size default - wordpress

I was wondering if any could help on my problem
I have this code from someone that runs fine except that the size does not function, default is always on "thumbnail"
function my_gallery_default_type_set_link( $settings ) {
$settings['galleryDefaults']['link'] = 'file';
$settings['galleryDefaults']['columns'] = '4';
$settings['galleryDefaults']['size'] = 'large';
return $settings;
}
add_filter( 'media_view_settings', 'my_gallery_default_type_set_link');
how can I make this always in large as a default?

This piece of code is actually working, the size of the gallery will be "large" by default if an other size is not manually selected. The real problem come from the dropdown itself that is not correctly set on initialisation, still in WP 4.8.2.
There is a ticket open with more details about this display error.
In the meantime, I found a workaround using the print_media_templates hook :
Step 1 - Define your gallery default image size
function my_gallery_default_settings( $settings ) {
$settings['galleryDefaults']['size'] = 'large';
return $settings;
}
add_filter( 'media_view_settings', 'my_gallery_default_settings');
Step 2 - Debug the dropdown image size default value
function debug_gallery_image_size_default_value() {
?>
<script>
jQuery(document).ready(function(){
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
template: function(view){
var base_view = wp.media.template('gallery-settings')(view);
var size_option_search = '<option value="'+wp.media.gallery.defaults.size+'">';
var size_option_replace = '<option value="'+wp.media.gallery.defaults.size+'" selected="selected">';
base_view = base_view.replace(size_option_search, size_option_replace);
return base_view;
}
});
});
</script>
<?php
}
add_action('print_media_templates', 'debug_gallery_image_size_default_value');

Why are you using someone else's custom code? There is already a Gallery shortcode with size option in it:
https://codex.wordpress.org/Gallery_Shortcode
Just call it with [gallery size="thumbnail"].

Actually, other code in other answers replaces default settings for existing galleries. Here's the code to apply default settings only to the new gallery:
add_filter( 'media_view_settings', 'theme_gallery_defaults', 10, 2 );
function theme_gallery_defaults( $settings, $post ) {
$defaults = ! empty( $settings['galleryDefaults'] ) && is_array( $settings['galleryDefaults'] ) ? $settings['galleryDefaults'] : array();
$settings['galleryDefaults'] = array_merge( $defaults, array(
'columns' => 5,
'size' => 'large',
'link' => 'file'
) );
return $settings;
}

Related

Undefined index when setting WordPress custom options variables?

I have my custom options set like this...
function my_options_init(){
register_setting(
'myoptions',
'myoptions',
'myoptions_validate'
);
}
add_action( 'admin_init', 'my_options_init' );
...and I'm used to setting variables on my custom settings page in this manner...
$myoptions = get_option( 'myoptions' );
$foo = $myoptions['foo'];
But when debugging is enabled I get an error:
Notice: Undefined index: foo
The solution as I understand it is to do this...
if(isset($myoptions['foo'])) {
$foo = $myoptions['foo'];
}
..which makes the error go away.
The problem is that I have many variables I want to use in my plugin, and it seems like a lot of unnecessary work to do this every time I need to use a variable.
Then I stumbled on this in another topic:
"...to avoid having to include this check everytime you are getting a
setting from $myoptions - would be to review your
myoptions() function and make sure it returns an
array that includes every setting, including default values for those
settings that are not saved in the database yet."
My question is, how would I go about doing that?
You can setup a function for default values. Then when you fetch plugin option, you can fetch options with defaults. After that even if there is no options in the database, function will get value from default. So no PHP notice. Please check following example.
function wpso_get_default_options() {
// Default plugin options.
$default = array(
'foo' => 1,
'bar' => 'left',
);
return $default;
}
function wpso_get_plugin_option( $key ) {
$defaults = wpso_get_default_options();
$plugin_options = get_option( 'myoptions' );
$plugin_options = wp_parse_args( $plugin_options, $defaults );
$value = null;
if ( isset( $plugin_options[ $key ] ) ) {
$value = $plugin_options[ $key ];
}
return $value;
}

Woocommerce add img tag on order admin details page

I have a wordpress website where customers make an image with text and icons, once processed thru woocommerce and payed for that image name 12345.png is saved as Customer_product_image
function add_order_item_meta($item_id, $values) {
$key = 'customer_product_image'; // Define your key here
$value = $values['user_img']; // Get your value here
woocommerce_add_order_item_meta($item_id, $key, $value);
}
And i works great, but now i'm banning my head against the wall! When the purchased image is displayed on the Order admin detail page, it shows up as CUSTOMER_PRODUCT_IMAGE: 1234.png how on earth would i go about wrapping that within an image tag so the image is displayed there?
I've searched high and low on google but haven't been able to find anything, its probably that i dont know what do actually search for....
This did the trick for me!
First i added this snippet for removing the custom meta item on order detail render:
add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );
function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}
second i added it back with this, and with the desired text and image tag:
add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
function order_meta_customized_display( $item_id, $item, $product ){
$all_meta_data=get_metadata( 'order_item', $item_id, "", "");
$useless = array(
"_qty","_tax_class","_variation_id","_product_id","_line_subtotal","_line_total","_line_subtotal_tax","_line_tax","_line_tax_data"
);// Add key values that you want to ignore
$customized_array= array();
foreach($all_meta_data as $data_meta_key => $value) {
if(!in_array($data_meta_key,$useless)){
$newKey = ucwords(str_replace('_'," ",$data_meta_key ));//To remove underscrore and capitalize
$customized_array[$newKey]=ucwords(str_replace('_'," ",$value[0])); // Pushing each value to the new array
}
}
if (!empty($customized_array)) {
foreach($customized_array as $data_meta_key => $value){
echo "<div class='product_container'><span>Produkt Billede: </span><img src='".wp_upload_dir()['baseurl'].'/flapper/'. $value ."' /> </div>";
}
}
}
i found the answer to this question on this page
You can use the filter woocommerce_order_item_display_meta_value to output the image. Place this code in your functions.php file, you'll need to modify the src attribute of the img tag to include the appropriate URL before the filename value. You can also modify the display label with the filter woocommerce_order_item_display_meta_key
add_filter( 'woocommerce_order_item_display_meta_value', 'modify_order_item_display_value' , 10, 3 );
function modify_order_item_display_value( $display_value, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return '<img src="' . $meta_data['value'] . '">';
}
return $display_value;
}
add_filter('woocommerce_order_item_display_meta_key', 'modify_order_item_display_key', 10, 3);
function modify_order_item_display_key( $display_key, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return 'Customer Image';
}
return $display_key;
}

How to add screen option checkbox for wordpress?

For my plugin I added a screen option with number input field. I want to add checkbox fields. Could not found out anything here. See the code:
//defined $my_admin here
add_action( 'load-' . $my_admin, "my_add_screen_options" );
//add screen options
function my_add_screen_options() {
$screen = get_current_screen();
//some checks
$args = array(
'label' => __('description', 'my'),
'default' => 20,
'option' => 'my_per_page'
);
add_screen_option( 'per_page', $args );
}
//Save the data from screen options
add_filter( 'set-screen-option', 'my_set_screen_option', 10, 3 );
function my_set_screen_option( $status, $option, $value ) {
if ( 'my_per_page' == $option ) return $value;
}
It works for number input. I thought changing $args will make it display checkboxes or any other field. But could not find anything significant by google too.
Can anybody help?
Using filter hook create one option in screen option.
For example i create Thumbnail option in post list. following code add in theme function.php
add_filter('manage_posts_columns', 'posts_columns', 5);
function posts_columns($defaults){
$defaults['my_post_thumbs'] = __('Thumbnail');
return $defaults;
}

How to Display Custom meta field value insted of title on custon post list table?

I have created a custom post type "cinfo" and removed title and editor form the edit page. With the help of this code. Also displayed some custom meta fields which are relevant to my plugin.
function remove_box(){
remove_post_type_support('cinfo', 'title');
remove_post_type_support('cinfo', 'editor');
}
add_action("admin_init", "remove_box");
It looks something like this.
Now when i see the list page I still see the title with edit, view and delete button beneath it. which I don't want because the title field doesn't exist in the edit page So it looks a bit irrelevant in the listing page. Instead of that I tried to display the custom meta field "email" but I was only able to change the heading of the column. which looks something like this.
I just did some research and found one action and filter but they still didn't seems to be much of a help to me. Still for the better view of the problem. Also I tried to use a plugin Post List View Count but it also didn't accomplish my purpose. I hope You understand what I basically want to do. Thanks for your time to read my question.
add_filter('manage_cinfo_posts_columns', 'bs_cinfo_table_head');
function bs_cinfo_table_head( $defaults ) {
$defaults['title'] = 'Email';
return $defaults;
}
add_action( 'manage_cinfo_posts_custom_column', 'card_cinfo_table_content', 10, 2 );
function card_cinfo_table_content( $column_name, $post_id ) {
if ($column_name == 'title') {
echo "its working";
}
}
The action manage_cinfo_posts_custom_column will never be true. It's better to remove the defaults on manage_cinfo_posts_columns and do the regular custom stuff in the other filter.
I tried this with a fast class to test all together inside my setup:
class SO23467344
{
private $cpt = 'portfolio';
private $custom_field = 'video';
public function __construct()
{
add_filter( "manage_edit-{$this->cpt}_columns", array( $this, 'column_register' ), 20, 1 );
add_action( "manage_{$this->cpt}_posts_custom_column", array( $this, 'column_display' ), 20, 2 );
add_action( "admin_init", array( $this, "remove_box" ) );
}
function column_register( $columns )
{
$columns['my-col'] = 'My column';
# Remove default columns
unset( $columns['title'], $columns['categories'], $columns['comments'], $columns['date'] );
return $columns;
}
function column_display( $column_name, $post_id )
{
if ( 'my-col' != $column_name )
return;
if ( $field = get_post_meta( $post_id, $this->custom_field, true ) )
echo '<br/><strong style="color:#0f0;font-size:4em"> • </strong>';
}
function remove_box(){
remove_post_type_support( $this->cpt, 'title' );
remove_post_type_support( $this->cpt, 'editor' );
}
}
new SO23467344;

Override a function with child themes functions.php

As the title reads I'm trying to modify a function called by a parent theme in my child, I know that the child theme is set to be loaded beforehand so I'm curious if this is even possible?
My parent theme has a function called ajax_search_box() that I'd like to modify a query in, and I'd rather not modify the parent theme files in case I need to update it down the road.. what would be the best way to do this?
Also, for bonus points, how would I go about doing this with a widget as well? Thanks in advance!
function SearchFilter($query) {
// If 's' request variable is set but empty
if (isset($_GET['s']) && empty($_GET['s']) && $query->is_main_query()){
$query->is_search = true;
$query->is_home = false;
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function ajax_search_box() {
if (isset($_GET["q"]))
{
$q = $_GET["q"];
global $wpdb;
$q = mysql_real_escape_string($q);
$q = $wpdb->escape($q);
$query = array(
'post_status' => 'publish',
'order' => 'DESC',
's' => $q
);
$get_posts = new WP_Query;
$posts = $get_posts->query( $query );
// Check if any posts were found.
if ( ! $get_posts->post_count )
die();
//Create an array with the results
foreach ( $posts as $post )
echo $post->post_title . "|" . $post->ID . "\n";
}
die();
}
// creating Ajax call for WordPress
add_action( 'wp_ajax_nopriv_ajax_search_box', 'ajax_search_box' );
add_action( 'wp_ajax_ajax_search_box', 'ajax_search_box' );
the parent theme needs to check if(function_exists('ajax_search_box')) and if it doesn't exist then it will declare it.
If the parent theme checks to see if the function exists, then you can declare it first and have it do what you want.
If the parent theme does not check, get in touch with the theme author to see if they will throw that change in for the next update....and code it yourself too. That way when the theme updates then you will still be good to go.
Break free of functions.php, write your own plugin.
<?php
/**
* Plugin Name: Manipulate the Parent
* Requires: PHP5.3+
*/
add_action( 'after_setup_theme', function()
{
remove_filter( 'pre_get_posts','SearchFilter' );
// now add your own filter
add_filter( 'pre_get_posts', 'your_callback_for_your_filter' );
});
function your_callback_for_your_filter()
{
// do stuff
}

Resources