How to add wysiwyg editor in Wordpress meta box - wordpress

I'm creating a meta box for my custom post type. There are multiple fields where I would like to use wysiwyg editor rather than <textarea>. Is is possible to add multiple editors to a meta box?
I would really appreciate your help!
Many thanks.
Dasha

Here is full code example:
add_action( 'add_meta_boxes', function() {
add_meta_box('html_myid_61_section', 'TITLEEEEE', 'my_output_function');
});
function my_output_function( $post ) {
$text= get_post_meta($post, 'SMTH_METANAME' , true );
wp_editor( htmlspecialchars($text), 'mettaabox_ID', $settings = array('textarea_name'=>'MyInputNAME') );
}
add_action( 'save_post', function($post_id) {
if (!empty($_POST['MyInputNAME'])) {
$datta=sanitize_text_field($_POST['MyInputNAME']);
update_post_meta($post_id, 'SMTH_METANAME', $datta );
}
});
P.S. MUST-Recommendation from my experience:
Forget adding custom codes, use Advanced Custom Fields, it's excellent and simplify your life.

http://codex.wordpress.org/Function_Reference/wp_editor was by far the easiest method I found, built into Wordpress since 3.3 (so upgrade ;-) )

// for custom post type
function wo_second_editor($post) {
echo "<h3>Write here your text for the blue box on the right:</h3>";
$content = get_post_meta($post->ID, 'wo_blue_box' , true ) ;
wp_editor( htmlspecialchars_decode($content), 'wo_blue_box', array("media_buttons" => false) );
}
add_action('edit_form_advanced', 'wo_second_editor');
function wo_save_postdata($post_id, $post, $update) {
//...
if (!empty($_POST['wo_blue_box'])) {
$data=htmlspecialchars($_POST['wo_blue_box']);
update_post_meta($post_id, 'wo_blue_box', $data );
}
}
add_action('save_post', 'wo_save_postdata');
// Theme:
<div class="blue">
<?php
$content = get_post_meta(get_the_ID(), 'wo_blue_box' , true );
$content = htmlspecialchars_decode($content);
$content = wpautop( $content );
echo $content;
?>
</div>

But you need to replace presentation with nl2br() function as textarea in custom templates have the toogle JS issue, that removes all your <P> and <br/> tags and therefore all line breaks.

You can use the wordpress default text editor in the metabox using
add_action( 'edit_page_form', 'my_second_editor' );
function my_second_editor() {
// get and set $content somehow...
wp_editor( $content, 'mysecondeditor' );
}

Try the custom field template plugin http://wordpress.org/extend/plugins/custom-field-template/

This did the trick for me:
http://www.farinspace.com/multiple-wordpress-wysiwyg-visual-editors/
It's basically creating your textarea with an id, then calling from js:
tinyMCE.execCommand('mceAddControl', false, 'your_textarea_id');
Hope it helps!

First install TinyMCE Advanced plugin.
Second add "theEditor" class to your textarea like this
<textarea class="theEditor" name="custom_meta_box"></textarea>
Thats it
;)
Nabeel

Related

Is it possible to call a custom field from ACF or Pods into a function?

I am fluent in HTML and CSS but not so much in PHP. Any help would be appreciated.
I have a custom function in Wordpress as seen below:
function prefix_calculation_global_calculation_vars( $vars ) {
return array(
'setup_cost' => 200,
);
}
add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' );
Is it possible to call an Advanced Custom Fields (ACF) field or a Pods field and have the value inserted into the function above where the 200 is?
Providing get_the_ID() works you could try this:
function prefix_calculation_global_calculation_vars( $vars ) {
return array(
'setup_cost' => get_field('setup_cost', get_the_ID()),
);
}
add_filter( 'pewc_calculation_global_calculation_vars', 'prefix_calculation_global_calculation_vars' );
You would have to grab it via the $wpdb->postmeta table
<?php echo get_post_meta(get_the_ID(), 'setup_cost', TRUE); ?>

Wordpress custom button link output after the_content

I'm trying to insert a button to show at the end of each post on Wordpress in which the link it goes to is defined by a setup using the custom fields plugin. When creating each post, I am able to select the link I wish to display.
Here is the code I have which I know is wrong but I was hoping someone could help here.
function wpb_after_post_content($content){
if (is_single()) {
$content .= 'Contact Franchise →';
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
I assume $franchise_profile_url is a variable and you should concatenate it in the string like this
$content .= 'Contact Franchise →';
function afterContent($content) {
if(!is_feed() && !is_home()) {
$content.= "<div class='footNote'>";
$content.= "<h4>Click the below button if you like it:</h4>";
$content.= "<p><a href='#'>LIKE</a></p>";
$content.= "</div>";
}
return $content;
}
add_filter ('the_content', 'afterContent');
Use the above function. It will help you to achieve what you need.
Thanks for the help here, however, that code simply links back to the post itself and isn't pulling in the URL as set on the post using custom fields. This is the code I had set up before which was working on a default post setup but now I wish to use an alternative method in the functions.php file
<?php if( get_field('franchise_profile_url') ): ?>
Contact Franchise →
<?php endif; ?>

How to display shortcode of specific function on Ultimate Member tab plugins

I have add this code to my functions.php for displaying Woocommerce Recent Orders:
//display recent orders
function woocommerce_orders() {
$user_id = get_current_user_id();
if ($user_id == 0) {
return do_shortcode('[woocommerce_my_account]');
}else{
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', $user_id),
'order_count' => $order_count
) );
return ob_get_clean();
}
}
add_shortcode('woocommerce_orders', 'woocommerce_orders');
Now how to display that "Woocommerce Recent Orders" of [woocommerce_orders] function on Ultimate Member tab?
I'll appreciate for your answers. Thanks...
Use this on your plugin template:
<?php echo do_shortcode('[woocommerce_my_account]'); ?>
you can render the shortcode using echo do_shortcode, and later with jQuery/JS append the HTML into the div you need to show the content.
Another way to do it is to edit directly the plugin's files.
/ultimate-member/templates/profile.php you can find the template file.just add your function there and it will render.
Also you can add a filter on your theme's function.php to override the plugin.
If you modify the plugin be sure to null it by changing the version and name of the plugin to 99.99.99 -to avoid automatic updates to override your changes.

Open WooCommerce External Products in New Tab

I'm trying to customize WooCommerce external product links to open in new tabs...
This is my try:
added a filter to the WordPress theme functions.php file as the following:
add_filter( 'woocommerce_product_add_to_cart_url', 'woocommerce_externalProducts_openInNewTab' );
function woocommerce_externalProducts_openInNewTab($product_url) {
global $product;
if ( $product->is_type('external') ) {
$product_url = $product->get_product_url() . '"target="_blank""';
}
return $product_url;
}
What did I missed?
what you're currently doing is wrong... get_product_url is named as what it do. It will give you the url... not the html anchor that has the url, but just the url.. so you're just adding some text to the url.. that's what you are doing...
One solution is given by #Ash Patel. You can change the markup by using templates... just navigate to your plugin folder and look for this file.. woocommerce\templates\single-product\add-to-cart\external.php. You can find instructions inside it.
Now, sometimes, we don't like editing templates... especially if it's just minor edits like this...
Below code will do it the way you want it... just paste this code in your theme's functions.php.
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'rei_external_add_to_cart', 30 );
function rei_external_add_to_cart(){
global $product;
if ( ! $product->add_to_cart_url() ) {
return;
}
$product_url = $product->add_to_cart_url();
$button_text = $product->single_add_to_cart_text();
do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<p class="cart">
<?php echo esc_html( $button_text ); ?>
</p>
<?php do_action( 'woocommerce_after_add_to_cart_button' );
}
Here is how you add target="_blank" to the links on archive pages:
function ns_open_in_new_tab($args, $product)
{
if( $product->is_type('external') ) {
// Inject target="_blank" into the attributes array
$args['attributes']['target'] = '_blank';
}
return $args;
}
add_filter( 'woocommerce_loop_add_to_cart_args', 'ns_open_in_new_tab', 10, 2 );
Replace ns_ part with your own namespace abbreviation.
Remove above funtion from function.php:
Use plugin files from Template folder by Template Overwrite method and then
follow below path:
woocommerce\templates\single-product\add-to-cart\external.php
open external.php where there is a tag, apply target="_blank".
it will work.

Shortcode in wordpress post is not showing in proper place

I have generated these shortcodes.Whenever I am using these short codes in some particular place posts and pages are not showing in the right place.It comes at the beginning of the content.
Can anyone help me out here?
function __construct()
{
register_activation_hook( __FILE__, array($this,'my_auction_creator_activation' ));
add_action( 'add_meta_boxes', array($this,'my_auction_creator_custom_field' ));
add_action( 'save_post', array($this,'my_auction_creator_form_save' ));
add_shortcode('myauctioncreator_listing' , array($this,'ebay_listing' ));
add_shortcode('myauctioncreator_ads' , array($this,'ebay_ads' ));
add_shortcode('myauctioncreator_profile' , array($this,'ebay_profile' ));
add_shortcode('myauctioncreator_feedback' , array($this,'ebay_feedback' ));
}
To render the output of the shortcode, are you using print or echo?
If you are, you should be returning the content instead. Using print or echo will cause this issue.
i was getting exactly same issue as i was echo the contents
use like this
function shortcode_function()
{
ob_start();
//some echo or html code goes here
echo "<p>this is paragraph</p>";
return ob_get_clean();
}

Resources