So i'm using Wordpress, Woocommerce and the "Product Video for WooCommerce" plugin by Addify. The URL to a mp4 video is stored in the:
wp_postmeta table
In this table, the post_id matches the product.
In the "meta_value" i can see the URL i've added.
No my question;
I want to place a download button that downloads the video stored in this location.
I've located the woocommerce hook where the button needs to come, but I can't figure out how to fetch the url from this meta_value.
My code skills are very basic so this is to complicated for me.
Can anyone help me out on this?
This showed me that the URL is visible but surely not the end goal :-)
add_filter( 'woocommerce_share', 'custom_button', 20 );
function custom_button() {
$meta = get_post_meta(get_the_ID(), '', true);
print_r($meta);
}
//Here I want to add the button
print '<button>Download video</button>';
Thanks!
'woocommerce_share' hook is an action, not filter.
Try this
add_action( 'woocommerce_share', 'custom_button', 20 );
function custom_button() {
$download_url = get_post_meta(get_the_ID(), 'afpv_cus_featured_video_id', true);
if ( empty( $download_url ) ) { // do not print anything if download_url doesn't exists
return;
}
// print the download button
printf( '<a href="%s" class="button" target="_blank" download>%s</a>',
esc_url( $download_url ),
__( 'Download video', 'text-domain' ) // string can be translated, change text-domain by the theme domain or plugin domain
);
}
Related
I have a plugin on a page update-data which has a shortcode on the page of [updatedata]
here is what an update row looks like
<td>UPDATE</td>
Here is what the plugin code is
<?php
/*
plugin name: deano plugin update
description: deano test database to update data into books table
author: Dean-O
*/
$path = preg_replace('/wp-content.*$/', '', __DIR__);
require_once($path.'/wp-load.php');
error_log("here"); // echos to log file
error_log(var_dump($id)); // echos a blank line to the log file
error_log("here 2"); // echos to the log file
function deanoupdatedata($atts, $content = null ) {
$a = shortcode_atts( array(
'id' => 'id'
), $atts );
return '<a id="' . esc_attr($a['id']) . '</a>'; // never displays
}
add_shortcode('updatedata','deanoupdatedata');
?>
How can I get the url parameter id in the plugin when I click on update link?
Try if($_GET['id']) instead of isset and istead of error_log use var_dump to get the exact value
It is a bad practice. You should pass the data to a shortcode using the shortcode's attributes.
I want to change to URL of the "Proceed to checkout" button. I wanna do this to to check if there's a need for my product, so my customers shouldn't be able to really buy it. Therefore I want to direct them not to the checkout but to a customized page of mine.
Thanks a lot.
Josh
It should be available at below path
wp-content/plugins/woocommerce/templates/cart/proceed-to-checkout-button.php
the best way is the woocommerce hooks
Try to use this code (put it in your functions.php theme file).
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$url = "your checkout url ";
return $url;
}
You can customize it by adding some conditions to change the checkout url
for example:
add_filter( 'woocommerce_get_checkout_url', 'my_change_checkout_url', 30 );
function my_change_checkout_url( $url ) {
$allowed_countries = array('FR');
$customer_country = WC()->customer->get_default_country();
if( !in_array( $customer_country , $allowed_countries ) ) {
$url = wc_get_page_permalink( 'other-checkout' );
}
return $url;
}
Change Proceed To Checkout Text In WooCommerce
* Change Proceed To Checkout Text in WooCommerce
* Place this in your Functions.php file
This is the new method, for version 2.3.8 of WooCommerce and forward.
<?php
function woocommerce_button_proceed_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
on my baked goods site, I have WooCommerce installed and another plugin called Order Delivery Date for WooCommerce.
I installed the second plugin so my customers would be able to choose a delivery date for their items, however, I am trying to make the form field a required field. So far, I've just been able to make the field look like a required field, but have not figured out how to make sure that it is actually enforced. Any ideas?
Also, if anyone is familiar with WooCommerce, do you know how I would be able to make it so that customers receive this delivery date information in their order confirmation emails?
Thank you in advance!
My site: www.monpetitfour.com
You should try to had something like that :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// You can make your own control here
if ( ! $_POST[ 'e_deliverydate' ] )
wc_add_notice( __( 'Please select a delivery date' ), 'error' );
}
For the email, the easiest is to save the meta value ( I think it's already done by your plugin). Then you need to copy the template email (customer-processing-order.php) on your theme and change in the template :
<?php $delivery_date = get_post_meta( $order->id, 'custom_field_date', true);
// If the plugin is well developed, you can't directly use magic getters :
// $delivery_date = $order->e_deliverydate;
// Can only by use if the post meta start with _
?>
Your delivery date is <?php echo $delivery_date ?>
You can also use
date_i18n( woocommerce_date_format(), strtotime( $delivery_date ) );
In order to format the date correctly.
On the code above, you just need to find the name of the custom field used by the plugin ( you can search easily on the table wp_postmeta searching by an existing order (should be _e_deliverydate).
Add the following code to your theme's functions.php file
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['e_deliverydate'] )
wc_add_notice( __( 'Please select a delivery date.' ), 'error' );
}
Now to get the email to show the custom field,
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['Delivery Date'] = '_e_deliverydate';
return $keys;
}
EDIT : Seems the field value isn't being saved to the database, try saving it explicitly
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['e_deliverydate'] ) ) {
update_post_meta( $order_id, '_e_deliverydate', sanitize_text_field( $_POST['e_deliverydate'] ) );
}
}
i want to force the attribute "alt" on thumbnail when admin add new thumbnail on wordpress.
if there is javascript hook on saving thumbnail validator on the input "title" that's well be great!
Thank you.
Found this, place it in your functions.php file in your theme directory
function add_alt_tags($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
if(!preg_match('/alt=/', $value))
{
$new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
add_filter('the_content', 'add_alt_tags', 99999);
Find more here http://www.paulund.co.uk/add-missing-alt-tags-to-wordpress-images
This is going on my snippet site. Hope it helps
By default, the image returned does not have a title or alt attribute. (Since WordPress 4.7, the alt attribute is no longer added automatically. It will only have an alt attribute if you specifically entered "Alt text" while uploading the image, or if you go back to the Media library and enter "Alt text" for the image).
Currently website(s) traffic from Google search is considerably higher if your images all have (title attribute tags), as well as (alt tags). So, I add the title and alt attributes to post thumbnails with the following function which goes in your theme "functions.php" file. The value for the title and alt attributes will be taken from the title of the image, which is the title of the attachment (not the actual post title).
function eln_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
$attr['title'] = $img_title;
$attr['alt'] = $img_title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','eln_add_img_title', 10, 2
);
If you don't want the image attributes taken from default image name
you can change the code so the image attributes taken from
"post_title" as follow:
// Force adding missing image alt & title for WordPress.
function eln_add_img_title( $attr, $attachment = null ) {
$img_title = trim( strip_tags( $attachment->post_title ) );
$attr['title'] = the_title_attribute( 'echo=0' );
$attr['alt'] = the_title_attribute( 'echo=0' );
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 );
Hope this help you and save your time, Have a nice day :)
i'm trying to learn the way for uploading image file through custom field but cant get the easiest code to do it. i just done a bit here:
add_action('admin_init', 'create_image_box');
function create_image_box() {
add_meta_box( 'meta-box-id', 'Image Field', 'display_image_box', 'post', 'normal', 'high' );
}
//Display the image_box
function display_image_box() {
global $post;
$image_id = get_post_meta($post->ID,'xxxx_image', true);
echo 'Upload an image: <input type="file" name="xxxx_image" id="xxxx_image" />';
// Upload done: show it now...(as thmbnail or 60 x 50)
anybody please take me to next step and show the way to display the image in blog page too.
Lets go Stepwise here:
Create custom field Meta Box for inserting Image Url in post type => post.
Update/Save the custom field value in back end.
Display the custom field value in front end.
Seeing your code it seems that you are missing #2. Try the code below to save custom field:
function save_joe_details($post_id){
global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
update_post_meta($post->ID, "custom_field_image", $_POST["custom_field_image"] );
}
add_action('save_post', 'save_joe_details');
Code for #3 that displaying the custom field will be:
<?php global $post;
$custom_image = get_post_custom($post->ID); ?>
<img src="<?php echo $custom_image["custom_field_image"][0] ?>" />
you could try wrap it in wp_get_attachment_url like this:-
wp_get_attachment_url( $custom_image["custom_field_image"][0] );