How to add screen option checkbox for wordpress? - 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;
}

Related

changing the wordpress gallery image size default

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

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;

wordpress moving publish metabox

I'm working on a plugin and I'm trying to:
1- move the publish metabox from side to the bottom of the page in the normal section.
2- force 1 column layout for the plugin custom post type edit page.
3- remove the screen options tab for the custom post type.
I'm using the next code block but it dont work:
function sds_do_meta_boxes() {
remove_meta_box( 'submitdiv', 'sliding_panel', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sliding_panel', 'normal', 'core' );
}
add_action('do_meta_boxes', 'sds_do_meta_boxes');
function SDS_init() { //The current user is already authenticated by this time
add_filter( 'screen_layout_columns', 'so_screen_layout_columns' );
add_filter( 'get_user_option_screen_layout_dashboard', 'so_screen_layout_dashboard' );
add_filter( 'screen_options_show_screen', 'SDS_remove_screen_options_tab');
}
add_action( 'init', 'SDS_init' );
function so_screen_layout_columns( $columns ) {
$columns['dashboard'] = 1;
return $columns;
}
function so_screen_layout_dashboard() {
return 1;
}
function SDS_remove_screen_options_tab() {
return false;
}
So, the 'publish' metabox is removed but it is not re-added. Also, the 1 column layout filters don't work. I need help:)
Changing the $priority parameter from core to high and setting priority of add_action to 0 should do the trick. I would also suggest using the dynamic add_meta_boxes_{$post_type} hook:
add_action( 'add_meta_boxes_sliding_panel', 'sds_do_meta_boxes', 0, 1 );
function sds_do_meta_boxes( $post )
{
remove_meta_box( 'submitdiv', 'sliding_panel', 'side' );
add_meta_box( 'submitdiv', __( 'Publish' ), 'post_submit_meta_box', 'sliding_panel', 'normal', 'high', null );
}

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
}

How to exclude password protected posts in WordPress loop

I have a custom post type that supports password protected entries. In a custom loop using a new WP_Query object, I want to exclude those password protected posts from the results. What arguments do I need set in order to do this? I am using the latest trunk version of WordPress 3.2.1.
I come up to this question where I was looking for the same. However, I read WP_Query document line by line then found very simple solution and that is just to add 'has_password' => false argument to the query $args
So the code will be as below...
$args = [
'post_type' => [ 'post', 'page' ],
'posts_per_page' => 3,
'post__not_in' => get_option( 'sticky_posts' ),
'has_password' => FALSE
];
Here you can see I have excluded Sticky and Password Protected posts.
I really like Kevin's approach, but I adjusted it slightly:
// Create a new filtering function that will add our where clause to the query
function my_password_post_filter( $where = '' ) {
// Make sure this only applies to loops / feeds on the frontend
if (!is_single() && !is_admin()) {
// exclude password protected
$where .= " AND post_password = ''";
}
return $where;
}
add_filter( 'posts_where', 'my_password_post_filter' );
Did you take a look at the post_status argument of WP_Query?
"Protected" seems like a good candidate to exclude.
Edit: Okay, it seems like you'll have to modify the where clause to achieve what you want:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// exclude password protected
$where .= " AND post_password = ''";
return $where;
}
if (!is_single()) { add_filter( 'posts_where', 'filter_where' ); }
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
After a bit of playing about, I found the posts_where filter a bit too intrusive for what I wanted to do, so I came up with an alternative. As part of the 'save_post' action that I attached for my custom post type, I added the following logic;
$visibility = isset($_POST['visibility']) ? $_POST['visibility'] : '';
$protected = get_option('__protected_posts', array());
if ($visibility === 'password' && !in_array($post->ID, $protected)) {
array_push($protected, $post->ID);
}
if ($visibility === 'public' && in_array($post->ID, $protected)) {
$i = array_search($post->ID, $protected);
unset($protected[$i]);
}
update_option('__protected_posts', $protected);
What this does is hold an array of post id's in the options table where the post is protected by a password. Then in a custom query I simply passed this array as part of the post__not_in option e.g.
$query = new WP_Query(array(
'post_type' => 'my_custom_post_type',
'post__not_in' => get_option('__protected_posts'),
));
This way I could exclude the protected posts from an archive page but still allow a user to land on the password protected page to enter the password.
In addition to #Peter Chester's answer:
You may also want to exclude password-protected posts from the Previous Post and Next Post links, if you have those at the bottom of your post page.
To do so you can add the exclusion to the get_previous_post_where and get_next_post_where hooks.
add_filter( 'get_previous_post_where', 'my_theme_mod_adjacent' );
add_filter( 'get_next_post_where', 'my_theme_mod_adjacent' );
function my_theme_mod_adjacent( $where ) {
return $where . " AND p.post_password = ''";
}

Resources