WordPress "in_widget_form" does not display basic HTML Code - wordpress

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.

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.

Changing the Purchase Button - Woocommerce

I have to hide the possibility to buy for not logged in users (whole domain or store)
Ex:
If a user is NOT logged in (don't have an account) then the cart button etc will not show. Sees only a link "See store locator list".
Only if user logged in will they show prices and button "Buy" and they will have the opportunity to purchase.
example code
if(is_user_logged_in()
Buy
if(is_user_NOT_logged_in()
Show "See store locator list" with page URL.
How to do it?
Please could someone help?
I found this example: Function Reference/is user logged in
Display different output depending on whether the user is logged in or not.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>
Did it change in the "/WooCommerce/Templates/Loop/add-to-cart.php" enough?
But still need help :(
How to put this code into "buy" button?
Your code isn't exactly set up for your needs. It would be more like
<?php if (is_user_logged_in()) {?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<?php
if ( ! $product->is_sold_individually() )
woocommerce_quantity_input( array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
) );
?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php } else { ?>
<div>Store Location</div>
<?php } ?>
Inside the div/form (doesn't need to be a div, but I just put there as an example) you can put whatever code you need.
I put in the form code for displaying the simple button. You would likely need this in all your add-to-cart templates. I'm also not entirely positive what is in the before/after add to cart button so those locations might need to be tweaked.

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" : ""; ?>

Wordpress Widget Textarea Auto Paragraph

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.

Wordpress plugin form issue

I am new to wordpress plug in development. I have designed a search form however I have no idea where to handle and print the submitted form data.
it is a wedget based plug in and the plugin form section code is here:
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$message = apply_filters( 'widget_content', $instance['content'] );
echo $before_widget;
//if ( $title )
// echo $before_title . $title . $after_title;
echo '<div class="shk_location_form_holder">
<span class="shk_loc_title">'.$title.'
<form mthod="post">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form></div>';
echo $after_widget;
if(isset($_REQUEST['shk_inp_search_locations'])){
add_filter('the_content','handle_content');
}
}
In WP plugins you usually have an empty action="" in a form, and handle it in the same function (by the way, as wordpress procedural code becomes very messy, it's better to write plugins using OOP), because, anyway plugins are loaded before any content is outputted in WP (this is the reason why writing ajax plugins is so easy in wp). So you can have everything structured like this:
function draw_form() {
handle_submit();
?>
<div class="shk_location_form_holder">
<span class="shk_loc_title"><?php echo $title; ?></span>
<form mthod="post" action="">
<input type="text" name="shk_inp_search_locations" id="shk_inp_search_locations" /><br>
<div style="height:5px"></div>
<input type="submit" Value="Search Locations" />
</form>
</div>
<?
}
function handle_submit() {
if(isset($_POST['shk_inp_search_locations']) && $_POST['shk_inp_search_locations'] == 'test') {
echo 'you may want to end your program here, especially if it\'s ajax!';
exit;
}
}

Resources