How to remove custom meta box on click of a button? - php

I'm trying to remove a custom metabox that I've created for my plugin using PHP code. It should be removed from all the posts on click of a button.
Here is my code:
<?php
if(isset($_REQUEST['submit_btn']))
{
function remove_custom_metabox()
{
remove_meta_box( 'my-meta-box-id' , 'post' , 'normal' );
}
add_action( 'add_meta_boxes', 'remove_custom_metabox');
}
?>
Why is it not working? And is there any way to do this for multi-post custom meta-box as well? Thanks!
EDIT 1: Just to get more clear idea of what I'm doing, here is how I'm creating the custom meta-box in the main plugin file:
function cd_meta_box_add()
{
add_meta_box(
'my-meta-box-id', //id
'Contributors', //title
'cd_meta_box_cb', //callback
'post', //post type
'normal', //position
'high' //priority
);
}
add_action('add_meta_boxes', 'cd_meta_box_add');

According to documentation you should use the admin_menu hook

To remove custom meta box use action hook admin_menu or do_meta_boxes
/**
* Remove Custom Fields meta box
*/
function wpdocs_remove_post_custom_fields() {
remove_meta_box( 'postcustom' , 'post' , 'normal' );
}
add_action( 'admin_menu' , 'wpdocs_remove_post_custom_fields' );
add_action( 'do_meta_boxes', 'wpdocs_remove_post_custom_fields' );
For more help see this link : click here

Related

On a WordPress page template, what code can customize title and meta description?

On custom-page-template.php, what code can customize the section ( and <meta name=”description”) for all pages which are based on this template?
I tried using the following but they nothing on a page template. (They work on functions.php though).
add_filter( 'document_title_parts', 'custom_document_title_parts' );
add_action( 'wp_head', 'my_add_meta_description');
Any idea, what code can customize and in a custom-page-template.php?
Thanks.
Use this in functions.php: (change page template's name in is_page_template)
add_filter( 'document_title_parts', function( $title_parts_array ) {
if ( is_page_template('custom.php') ) {
$title_parts_array['title'] = 'New title';
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description");</script>';
}
return $title_parts_array;
} );
Or you can use this before get_header() in your custom page template
function new_page_title() {
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description2");</script>';
return 'New Title2';
}
add_action( 'pre_get_document_title', 'new_page_title' );

Get template part in WordPress plugin

I'm trying to move a WordPress plugin from my child theme to a custom plugin. The current code is using get_template_part which works on the child theme but not on the custom WordPress plugin.
I have tried to replace it for "include" but it doesn't work.
function arbolesplantados_endpoint_content() {
get_template_part('arbolesplantados'); //I think the problem is here
}
add_action( 'woocommerce_account_arbolesplantados_endpoint', 'arbolesplantados_endpoint_content' );
function arbolesplantados_account_menu_items( $items ) {
$my_items = array(
'arbolesplantados' => __( 'Gestionar árboles plantados', 'woocommerce' )
);
$my_items = array_slice( $items, 0, 1, true ) +
$my_items +
array_slice( $items, 1, count( $items ), true );
return $my_items;
}
add_filter( 'woocommerce_account_menu_items', 'arbolesplantados_account_menu_items');
The previous code is supposed to create a page with the woocommerce menu (my account) on the left and then on the right print the custom form that's inside a file called arbolesplantados.php which is in the root folder of my plugin
You missed to register endpoint for the custom page. You can register endpoint like this.
function wpso_add_my_account_endpoint() {
add_rewrite_endpoint( 'arbolesplantados', EP_PAGES );
}
add_action( 'init', 'wpso_add_my_account_endpoint' );

Can't remove WooCommerce's image zoom

I am trying to remove the image zoom from my custom themed website which uses WooCommerce. Here is what I've tried adding in my functions.php file:
add_action( 'after_setup_theme', 'remove_pgz_theme_support', 100 );
function remove_pgz_theme_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
}
and this
add_action( 'wp', 'remove_pgz_theme_support', 20 );
function remove_pgz_theme_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
}
I've spend some time googling but the answers always recommend trying something along the lines of the above.
I disable all image effects with:
add_action('wp', 'remove_woo_image_effects');
function remove_woo_image_effects() {
// this should be disable the gallery slider and lightbox
remove_theme_support('wc-product-gallery-lightbox');
remove_theme_support('wc-product-gallery-slider');
// this should be disable the zoom
remove_theme_support('wc-product-gallery-zoom');
}
I tested it with DIVI and two from my custom themes
checked it here with custom theme
The solution that worked for me is adding the following to your functions.php file
// Add WooCommerce support
function add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'add_woocommerce_support' );

How to make custom post meta wp_editor translatable?

I have custom post type named soto_property in which I have added three wp_editor as post meta, using this code -
wp_editor( htmlspecialchars_decode($valueeee1),
'propertyEditor1',
$settings = array('textarea_name'=>'detail',
'editor_class'=>'propertyEditor')
);
wp_editor( htmlspecialchars_decode($valueeee2),
'propertyEditor2',
$settings = array('textarea_name'=>'features',
'editor_class'=>'propertyEditor')
);
wp_editor( htmlspecialchars_decode($valueeee3),
'propertyEditor3',
$settings = array('textarea_name'=>'text_to_pdf',
'editor_class'=>'propertyEditor')
);
Now I have installed qtranslate plugin to make my site Multilingual. This plugin automaticaly add Language tab in its default content editor. I want to add these languages tabs in my custom editor also, so it can save content in defined languages.
How can I do this.? Please help me.
add_filter('soto_property','qtrans_convertURL');
I guess you need to define translatable strings. Check this page in codex for more details.
Updated code should look like this:
$settings = array('textarea_name'=>__('detail', 'your-text-domain'),
'editor_class'=>'propertyEditor')
If it won't work for you, try the following trick.
Here's a code snippet from the last link:
if ( is_admin() && function_exists("qtrans_getSortedLanguages") ) {
add_action('admin_menu', 'enable_qTranslate_Meta');
}
function enable_qTranslate_Meta() {
global $qtransMETA;
$post_types = get_post_types();
/* post and page types are already processed by the plugin */
$disabled_types = array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item' );
$enabled_types = array_diff( $post_types, $disabled_types );
if ( $enabled_types ) {
foreach( $enabled_types as $enabled_type ) {
add_meta_box(
'qtrans_meta_meta_box', //HTML id
__('Multilingual META', 'qtranslate-meta'), //title
array(&$qtransMETA, 'meta_box_generate'), //callback
$enabled_type, //type
'normal', //context - normal, advanced, side
'high' //priority - high, low
);
}
}
}
Solved!
I have used qranslate-x plugin which makes my custom wp_editor translatable on my custom post type page.

Targeting a specific `<div>` via Wordpress Plugin

Insert HTML Input to a themes template file?
My question is this:
Currently with my code, I can have whatever I've set in my custom settings page appear on
the end of every post using, of course, a WordPress filter.
The only thing is, I don't want what I input to go anywhere within a post. What I am trying to
achieve, is to have my input injected into the current themes home.php template file, within a <div></div> tag that's contained within that template file. The <div> in question has an ID attached, which is <div id="category-inner">, so is there any way use it's ID to target it in my plugin?
I've successfully managed to do it, by editing the actual home.php template file and inserting a bit of php directly there to show the user input, but that totally goes against what I'm trying to do, which is to not have to edit the source code of the template file and only have to use my plugin to insert the users inputted text in that specific location (the <div> mentioned above).
My plugin is only ever going to be adding user input in one place on the site, and it will never change. And the place it will always go in, is where I mentioned above.
Below is my plugin code:
<?php
/*/
Plugin Name: Custom Text Adder
Plugin URI: N/A
Description: Demonstrates how rubbish I am at pretty much everything I want to do
Version: 101
Author: JD
Author URI: N/A
/*/
// insert custom plugin settings menu
add_action('admin_menu', 'custom_create_menu');
add_filter('the_content', 'customhead_the_content');
function customhead_the_content($content) {
$output = $content;
$output .= '<div id="category-inner">';
$output .= get_option('post_text');
$output .= '</div>';
return $output;
}
// Add Font-Size to WYSIWYG Editor
function wp_editor_fontsize_filter( $options ) {
array_shift( $options );
array_unshift( $options, 'fontsizeselect');
array_unshift( $options, 'formatselect');
return $options;
}
add_filter('mce_buttons_2', 'wp_editor_fontsize_filter');
// Create Custom Menu
function custom_create_menu() {
//create new top-level menu
add_menu_page('Custom Plugin Settings', 'Custom Settings', 'administrator', __FILE__, 'custom_settings_page',plugins_url('/img/icon.png', __FILE__));
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
// Register our settings
function register_mysettings() {
register_setting( 'custom-settings-group', 'new_option_name' );
register_setting( 'custom-settings-group', 'some_other_option' );
register_setting( 'custom-settings-group', 'option_etc' );
register_setting( 'custom-settings-group', 'font_size' );
register_setting( 'custom-settings-group', 'post_text' );
}
function custom_settings_page() {
?>
<!-- Custom Settings Page Container -->
<div class="wrap">
<h2>Custom Text</h2>
<form method="post" action="options.php">
<?php settings_fields( 'custom-settings-group' ); ?>
<table class="form-table">
<?php /* Bring the editor onto the page */
wp_editor( '', 'post_text', $settings = array() );
// 4.
// Custom buttons for the editor.
// This is a list separated with a comma after each feature
// eg. link, unlink, bold, ...
$settings = array(
'textarea_name' => 'post_text',
'media_buttons' => true,
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
'bullist,blockquote,|,justifyleft,justifycenter' .
',justifyright,justifyfull,|,link,unlink,|' .
',spellchecker,wp_fullscreen,wp_adv'
)
);
submit_button( 'Save everything', 'primary', 'submit' ); ?>
</table>
</form>
</div>
<?php }; ?>
Here is the screenshot which will be able to explain this in the most concise way possible:
http://i.imgur.com/hiEjEsA.jpg
Again, any and all help is hugely appreciated and will hopefully stop my brain from hurting!
You all rock,
Casey. :)
I'm not sure if you want this to be a pure PHP solution, but since you have the ID of the div you could target it with jQuery and insert the text into the div on page load (or form submit) with something like this:
$(document).ready(function() {
var userText = $('#input-id').val();
$('#category-inner').html(userText);
})

Categories