Wordpress Actions to Extend Plugin - wordpress

I newby here ... Woocommerce Wishlist plugin so that I can have the "add to wishlist" on my products page and not just the single page.
I added this to my functions file but it creates errors. I have done a ton of searching, but unable to find a solution.
Any ideas would be very much appreciated.
Code: add_action( 'woocommerce_after_shop_loop_item_title', array( WC_Wishlists_Plugin, 'add_to_wishlist_button' ), 10 );
Error: Warning: Use of undefined constant WC_Wishlists_Plugin - assumed 'WC_Wishlists_Plugin' (this will throw an Error in a future version of PHP)
Cheers

most probably your code is deprecated.
Try the bellow code and check if it would achieve the result that you wants.
It worked out for me but the design wasn't good enough
function addWishlistButton() {
global $wishlists;
?>
<form class="cart wishlist-cart-form" method="post" enctype='multipart/form-data'
action="<?php echo add_query_arg( array( 'add-to-wishlist-itemid' => get_the_ID() )); ?>">
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( get_the_ID() ); ?>" />
<?php $wishlists->add_to_wishlist_button(); ?>
</form>
<?php
}
add_action( 'woocommerce_after_shop_loop_item', 'addWishlistButton', 10 );
**PS: ** You need to add the code inside your functions.php

Related

WooCommerce One Page Checkout - Hide Product Attribute Colon

I'm currently using WooCommerce OnePage CheckOut
I'd like to remove the "attribute :" text from the product list.
I copied product-list.php
FROM: "/wp-content/plugins/woocommerce-one-page-checkout/templates/checkout/"
TO: "/public_html/wp-content/themes/my-theme-child/woocommerce/checkout/product-list.php"
Modified the "/public_html/wp-content/themes/my-theme-child/woocommerce/checkout/product-list.php" file
FROM:
<span class="attributes"><?php echo esc_html( apply_filters( 'woocommerce_attribute', $attribute_string, $product->get_variation_attributes(), $product ) ); ?></span>
TO:
<span class="attributes"><?php echo esc_html( apply_filters( 'woocommerce_attribute', str_replace ("attribute: ","",$attribute_string), $product->get_variation_attributes(), $product ) ); ?></span>
Once I saved the file, seems the changes not reflected on the page?
Did I miss anything here?
Thanks
It turns out to be caching issue sigh
All good now
Another option is to update /wp-content/themes/my-theme-child/functions.php
add_filter( 'woocommerce_attribute_label', 'remove_attribute_label');
function remove_attribute_label() {
return "";
}
But with this option, the colon still exists...

Wordpress plugin add_settings_field exact code works in one instance but not in a second instance?

I am writing a plugin making admin options pages using the combo "register_setting", "add_settings_section", "add_settings_field". I have a parent menu and 2 sub-menus. Making admin pages in Wordpress is pretty straight forward... it starts when you make the admin page. Here are my parameters for it. (this is done in OOP)
'parent_slug' => 'Tee_Off',
'page_title' => 'Tee-Off Reports',
'menu_title' => 'Tee-Off Reports',
'capability' => 'manage_options',
'menu_slug' => 'tee_offs_reports',
'callback' => 'path to my template file goes here'; //this is correct in my code
so remember my menu slug "tee_offs_reports" posted above is needed for the next step...
class ClubHouseReports
{
function register()
{
add_action( 'admin_init', array( $this, 'teeoff_report_sellector') );
}
function teeoff_report_sellector() {
register_setting( 'my_options_group', 'timer_month' );
register_setting( 'my_options_group', 'timer_day' );
register_setting( 'my_options_group', 'timer_year' );
add_settings_section( 'teeoff-club-options', 'Get Monthy Report', array( $this, 'teeoff_month_section'), 'tee_offs_reports');
add_settings_field( 'timer_month', 'Month', array( $this, 'teeoff_month'), 'tee_offs_reports', 'teeoff-club-options');
add_settings_field( 'timer_day', 'Day', array( $this, 'teeoff_day'), 'tee_offs_reports', 'teeoff-club-options');
add_settings_field( 'timer_year', 'Year', array( $this, 'teeoff_year'), 'tee_offs_reports', 'teeoff-club-options');
}
function teeoff_month_section() {
echo '<p>Reports</p>';
}
function teeoff_month() {
$timer_month = esc_attr( get_option( 'timer_month' ) );
echo '<input type="text" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';
}
function teeoff_day() {
$timer_day = esc_attr( get_option( 'timer_day' ) );
echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';
}
function teeoff_year() {
$timer_year = esc_attr( get_option( 'timer_year' ) );
echo '<input type="text" class="regular-text" name="timer_month" value="'.$timer_year.'" placeholder="Year" />';
}
}
This class above is being call from the root plugin file, just like the one that works. An instance is made then the method "register()" is called.
In my template file I do this.... just like the one that does work.
<?php settings_errors(); ?>
<form method="post" action="options.php">
<?php settings_fields( 'my_options_group' ); ?>
<?php do_settings_sections('teeoff-club-options'); ?>
<?php submit_button(); ?>
</form>
SO my problem is... except for where the values need to change when you repeat the same code, in this case like, you need to assign a new menu slug, a new name for the "register_setting", and making sure it's aiming at the right callback, which I assure you everything is set right... for some reason this wont render.
I get no errors, all I see is the submit button, and if I use it I get a "success" message, so everything is in scope... but nothing is rendering on the "tee_offs_reports" page except for the submit button... which makes sense since it's hard coded there.
All this to say... I went over my code dozens of time to look for the smallest/stupidiest syntax mistake, or anything why this admin area wont work, while the other which uses the exact same structure and logic, works 100%. Do you see anything wrong with this code? Don't mind the big space between code above, that only happened when I pasted it here.... Is there a restriction in wordpress that I don't know of yet? Why is this not working? While a blue print of it works just fine.
OK, now I'm really baffled... I got it to work, but I highly doubt I'm following the codex rules... or am I?????
here is the code that is working 100%
function teeoff_report_sellector() {
register_setting( 'my_options_group', 'timer_month' );
register_setting( 'my_options_group', 'timer_day' );
register_setting( 'my_options_group', 'timer_year' );
add_settings_section('tee-off-reports', 'Get Monthy Report', array( $this, 'teeoff_month_section'), 'tee-off-reports');
add_settings_field('timer_month', 'Month', array( $this, 'teeoff_month'), 'tee-off-reports', 'tee-off-reports');
add_settings_field('timer_day', 'Day', array( $this, 'teeoff_day'), 'tee-off-reports', 'tee-off-reports');
add_settings_field('timer_year', 'Year', array( $this, 'teeoff_year'), 'tee-off-reports', 'tee-off-reports');
}
function teeoff_month_section() {
echo '<p>Reports</p>';
}
function teeoff_month() {
$timer_month = esc_attr( get_option( 'timer_month' ) );
echo '<input type="date" class="regular-text" name="timer_month" value="'.$timer_month.'" placeholder="Month" />';
}
function teeoff_day() {
$timer_day = esc_attr( get_option( 'timer_day' ) );
echo '<input type="text" class="regular-text" name="timer_day" value="'.$timer_day.'" placeholder="Day" />';
}
function teeoff_year() {
$timer_year = esc_attr( get_option( 'timer_year' ) );
echo '<input type="text" class="regular-text" name="timer_year" value="'.$timer_year.'" placeholder="Year" />';
}
What I did is... instead of using the menu slug like the codex tells you to do, ...(and the first admin page I created works well that way) I replaced that by the "add_settings_section" ID... so if you look at the before last parameter for each "add_settings_field" they are now assigned the ID of the section, codex is supposed to be looking for the menu slug... I always thought anyways... again the first one I did, works awesome that way... so the 2 last parameters of the "add_settings_field" are the same. Weird! Nowhere is the page slug mention in the code above, yet this works 100% and does not break any other code.
Now I'm wondering... did I find a bug in the Settings API? that will be fixed later on, and my plugin is going to stop working? Or did I stumble on the solution by being desperate trying a whole bunch of stuff???
I don't get it... if there's a real WordPress guru out there that can explain this to me I would be very appreciative!

Why is this piece of code making two fields?

The problem is i need a custom field for my buyers to input some dates in a textarea, and when i make the code for it with a plugin or hardcode a function for it, the code makes 2 input fields as shown in this picture/gyazo, i i cant seem to figure out why :/
https://gyazo.com/97e61de41071324491268258d84f8aae
You can see the code in the next gyazo:
https://gyazo.com/e811efd8b0e9c60b361dc52027693797
I have tried everything that i could figure out with my compatebilities
//Custom Product Option
function custom_field_by_category() {
global $product;
if ( is_product_category( 'work-date' ) ) {
return;
}
?>
<div class="custom-date-field">
<label for="custom-date"><?php _e( 'Engraving (10 characters)', 'iconic' ); ?></label>
<input type="text" id="custom-date" name="dato_oensker" placeholder="<?php _e( 'Dato Ønsker', 'iconic' ); ?>" maxlength="10">
</div>
<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'custom_field_by_category', 10 );

do i have to sanitize wordpress options when i post per options.php?

Im writing a cookie notice plugin and im wondering if i need to sanitzie the inputs or escape the outputs of the options when i post / store the options via options.php and use the options via get_option().
i just want to create a safe plugin.
here is an example how i would handle the options:
function formz_register_settings()
{
add_option('formzname', 'guckguck');
register_setting('formz_settings', 'formzname', 'formz_callback');
}
add_action( 'admin_init', 'formz_register_settings' );
function formz_register_options_page() {
add_options_page('Page Title', 'FORMZ', 'manage_options', 'formz', 'create_admin_option_page');
}
add_action('admin_menu', 'formz_register_options_page');
function create_admin_option_page(){
?>
<h1>Formulare bearbeiten</h1>
<hr>
<form method="post" action="options.php">
<?php settings_fields('formz_settings'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="formzname">Facebook Pixel ID</label></th>
<td><input class="regular-text code" type="text" id="formzname" name="formzname"
value="<?php echo esc_html(get_option('formzname')); ?>"/></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<br>
<?php echo esc_html(get_option('formzname')); ?>
<?php
}
?>
then i use get_option('bla'); in the frontend to display the settings.
is options.php the right way for that or would be admin-post.php better for it ? and whats the difference ?
basicly i just want to know if my code is allright like that to publish.
thanks!
WordPress will ensure it's safe to save in the database by escaping the input when the query is run, however it's always best practice to ensure you sanitise it yourself using something like sanitize_text_field.
You can, however in your register_setting function call, define the sanitisation method to use when saving it so you don't need to worry about doing it yourself.
So your code here:
register_setting(
'formz_settings',
'formzname',
'formz_callback'
);
Can be changed to:
register_setting(
'formz_settings',
'formzname',
array(
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field'
)
);

Wordpress Shortcode in custom field within custom post type

Hi Guys Need help with this very badly.
Need to add shortcode to output in the area circled in white in the picture below.
And the input area is under video description. And from my understanding ive have confirmed that the name for that text area is description_value.
I have looked through every documentation and tried all filters and do_shortcode variations to no avail. Please help i have spent 3 days non stop doing this. Puting the codes in my function.php and so many others. It still does not parse [shortcodes] it just displays text "[shortcodes]". please refer to picture below
Thank you.
This is outputing on the page. I have
<div class="describe-feat">[postexpirator]</div>
This is in a file called grid-gallery.js
<h2><%= item.title %></h2></div><div class="view-gallery">\
<div class="describe-feat"><%=item.desc%></div>\
<% if(item.imgnum){ %><span class="item-num"><%= item.imgnum %></span><% } %>\
This is in custom post editor in wordpress admin area
<textarea name="description_value" class="option-textarea">[postexpirator]</textarea>
https://www.dropbox.com/s/almn09e1dwmeywb/shortcodxe.jpg
It's because, you are missing do_shortcode function for parsing shortcode.
Assuming you just want to target a single value, you could just do this inside the loop.
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , true ) ) ); ?>
If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array...
<?php $values = do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , false ) ); ?>
<?php if($values && is_array($values)) : ?>
<?php foreach( $values as $meta) : ?>
<p><?php echo $meta ?></p>
<?php endforeach; ?>
<?php endif; ?>
Just want to add that if you won't use the_post() no shortcode will work,
I had this issue when trying to enable shortcode on CutomFields on a new page type, and nothing worked until activating WP loop with the_post() .

Resources