Wordpress Widget Textarea Auto Paragraph - wordpress

I am in the works of throwing together what I believe to be a fairly simple widget for a client site, but being my second Widget ever I still have quite a bit of a learning curve to overcome.
The Story
In this particular project I have attempted to create some textarea inputs to output into some simple paragraph tags. But I kinda tackled it as logically as I could but I am sure it is pretty gross code as I am not a PHP expert by even the leanest standards.
The Issue
My issue is that I wish to have my textarea's output with auto paragraphing. Such as the default text widget wordpress provides when the checkbox is checked. Also maybe just have a look over at how I even created the textarea's. I am sure there is a better practice then what I put together.
Textarea Sample
<p>
<label for="<?php echo $this->get_field_id( 'admitted' ); ?>"><?php _e('Admitted to Practice', 'framework') ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id( 'admitted' ); ?>" name="<?php echo $this->get_field_name( 'admitted' ); ?>" value="<?php echo $this->get_field_name( 'admitted' ); ?>" /><?php echo $instance['admitted']; ?></textarea>
</p>
I don't know if maybe putting tinymce on them would help solve this or not (not that I know how to do that) But I am wide open to suggestions.
Take a look at the full code here.
Dropbox Zip of Widget

You know that you can easily look at how the default widgets are built, right? Just go to wp-includes/default-widgets.php
If I understand your issue correctly you are looking for this piece of code in the class WP_Widget_Text in that file,
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
<div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
As you can see, it is the wpautop function that does the trick.
I think the way you created your form is fine. If you want to learn best practices, again, Wordpress source code is useful.

I was able to accomplish this using the code below.
<p><strong>Admitted to Practice:</strong><br/><?php echo wpautop($this->admitted_practice); ?></p>
Thank you Calle for your response.

Related

add a separate payment button for each payment method in WooCommerce

I want to add a separate payment button for each payment method in WooCommerce.I designed checkout page using cart flows
I tried in chrome developers tool by copying button html content and place in every div of payment methods
but that was just temporarily changes..
I then go to plugin files editor and select cart flows plugin but don't know what's to do next
You should consider modifying the template files..
Locate the WooCommerce template files:
You can find the template files in the
"wp-content/plugins/woocommerce/templates" directory.
Copy the "checkout/payment.php" file:
Make a backup of the original file and then copy the "payment.php"
file to your theme directory.
Edit the copied file:
In the copied file, look for the section that displays the payment
methods and modify it to add separate buttons for each payment method.
Example code (That's an old woocommerce version I found in my computer and edited it) :
<input type="radio" id="payment_method_<?php echo esc_attr( $gateway->id ); ?>" class="input-radio" name="payment_method" value="<?php echo esc_attr( $gateway->id ); ?>" <?php checked( $gateway->chosen, true ); ?> />
<label for="payment_method_<?php echo esc_attr( $gateway->id ); ?>">
<?php echo wp_kses_post( $gateway->get_title() ); ?> <?php echo wp_kses_post( $gateway->get_icon() ); ?>
</label>
<div class="payment_box payment_method_<?php echo esc_attr( $gateway->id ); ?>" <?php if ( ! $gateway->chosen ) : ?>style="display:none;"<?php endif; ?>>
<?php $gateway->payment_fields(); ?>
</div>
Save and upload the file:
Save the changes and upload the edited file to your theme directory.

WordPress "in_widget_form" does not display basic HTML Code

I have been taking what I thought would be a simple HTML code. I tried to follow this tutorial here:
However, I am not able to get a simple input field to be added to the bottom of any widget. Could someone please tell me what I am missing?
After add below to my functions file, below does not output HTML of any thing withing wp-admin/widgets.php. Help please!
function thmfdn_add_menu_description_option( $widget, $return, $instance ) {
// Are we dealing with a nav menu widget?
if ( 'nav_menu' == $widget->id_base ) {
// Display the description option.
$description = isset( $instance['description'] ) ? $instance['description'] : '';
?>
<p>
<input class="checkbox" type="checkbox" id="<?php echo $widget->get_field_id('description'); ?>" name="<?php echo $widget->get_field_name('description'); ?>" <?php checked( true , $description ); ?> />
<label for="<?php echo $widget->get_field_id('description'); ?>">
<?php _e( 'Show descriptions', 'thmfdn_textdomain' ); ?>
</label>
</p>
<?php
}
}
add_action('in_widget_form', 'thmfdn_add_menu_description_option', 10, 3 );
My goal is to add visibility controls to widgets in order to dynamically display or high them on the frontend based on user logged in or not. To achieve that I want to extend existing widgets by adding extra settings that sets whether a widget should be rendered on frontend based on user status.
Thank you for any correction or solutions.

WordPress: Widget checkbox option not storing the state

I am having trouble to keep the state of Wordpress Widget option checkbox. Here is my code
<label for="<?php echo $this->get_field_id('display_name'); ?>"><input
type="checkbox"
id="<?php echo $this->get_field_id('display_name'); ?>"
name="<?php echo $this->get_field_name('display_name'); ?>"
<?php checked(isset($display_name) ? $display_name : 0); ?> /> Display name</label>
Also I want to know how can I use multiple checkbox group to store and retrieve data
Okay found the easy solution
Instead of $display_name in checked I simply changed to 1 and it works :)
Before:
<?php checked(isset($display_name) ? $display_name : 0); ?>
After:
<?php checked(isset($display_name) ? 1 : 0); ?>
I don't know why but this answer did not work for me, after reviewing the checked() documentation I tried to follow some of those example with no luck. So eventually I resulted to this simple snippet (just plain php) and it worked as expected.
<?php echo $checked = ($display_name=== "on") ? "checked" : ""; ?>

Overriding a theme function from plugin in WordPress

Is it possible to override a function in a theme, from a plugin? I've been struggling to find an example of how I would achieve the above.
If anyone could shed any light it would be really helpful.
edit: Adding some code
So this is my function which allows the cart in woocommerce to update using ajax:
add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cartplus_fragment', '1');
function woocommerce_header_add_to_cartplus_fragment( $fragments ) {
global $woocommerce;
$basket_icon = esc_attr($instance['basket_icon']);
ob_start();
?>
<a class="cartplus-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" onclick="javscript: return false;" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
<?php
$fragments['a.cartplus-contents'] = ob_get_clean();
return $fragments;
}
However, some themes also have a similar function built in - in these themes my code stops working which seems strange as I use the above code twice in my plugin (one is a variation of the above) and they work fine together. This is the code built in to the theme:
add_filter('add_to_cart_fragments', 'woocommerce_cart_link');
function woocommerce_cart_link() {
global $woocommerce;
ob_start();
?>
<a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" title="<?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> <?php _e('in your shopping cart', 'woothemes'); ?>" class="cart-button ">
<span class="label"><?php _e('My Basket:', 'woothemes'); ?></span>
<?php echo $woocommerce->cart->get_cart_total(); ?>
<span class="items"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count); ?></span>
</a>
<?php
$fragments['a.cart-button'] = ob_get_clean();
return $fragments;
}
Unless the function is meant to be overridden, no. This is basic PHP. You can't redefine a function. If you try you will get a fatal error.
Parts of WordPress are written to be overwritten. Look at /wp-includes/pluggable.php. Every function in there is wrapped in a if( !function_exists(...) ) conditional. Unless your theme did the same, and some do for some functions, you can't overwrite.
Look around for filters that might help you instead.
Looking at your code, you should be able to unhook that. Just make sure to hook the unhook late enough. That is not a good solution, though since you are breaking theme functionality and also must know the know the names of all the hooked functions that themes are using.
Is there something in $fragments, or in $_POST or $_GET or anything else, that you can use to conditionally run your code, leaving the rest alone.

Wordpress: How to get a thumbnail post plugin to work

I need a second opinion on something. I am trying to use the WP Post plugin as described in the following link:
http://www.seoadsensethemes.com/wordpress-wp-post-thumbnail-plugin/
You will see a section toward the bottom stating that you can call the feature using the following code:
$Wppt->get_post_thumbnail( $post->ID, 'post-thumbnail-square' );
It shows the following example inserted into the post loop:
<!-- wp post thumbnail -->
<?php if ( function_exists( "$Wppt->get_post_thumbnail" ) ) {
$thumb = $Wppt->get_post_thumbnail( $post->ID, 'thumbnail-custom-key-name' );
if ( !empty( $thumb ) ) { ?>
<img class="thumbnail" src="<?php echo $thumb['url']; ?>" title="<?php echo $thumb['title']; ?>" alt="<?php echo $thumb['alt']; ?>" width="<?php echo $thumb['width']; ?>" height="<?php echo $thumb['height']; ?>" />
<?php }
} ?>
<!-- wp post thumbnail -->
I cannot get this to work. Can someone tell me how this should specifically be used? I am simply pasting the code above my loop on the default theme, replacing the 'thumbnail-custom-key-name' with the one assigned to my thumbnail. Is this the right way to do it?
In your sample code it looks like you're using a default value 'thumbnail-custom-key-name' is that the name of an actual preset you've made?

Resources