How to add WordPress back button for single page only - wordpress

I would like to add a "back" button to a single page of my WordPress site. How can I do this? I found a lot of different code options but none for WordPress specifically that will work on one page.
I can't find the individual *.PHP file for a specific WordPress page, which I think would be required to implement something like this code:
<button type="button" onclick="history.back();"> Back </button>
I tried adding this to the "Text" tab in WordPress, but there apparently is no
history.back function built in.

try to add this code to your function.php
add_action( 'back_button', 'wpse221640_back_button' );
function wpse221640_back_button() {
if ( wp_get_referer() ) {
$back_text = __( '« Back' );
$button = "\n<button id='my-back-button' class='btn button my-back-button' onclick='javascript:history.back()'>$back_text</button>";
echo ( $button );
}
}
add this to your template page
<?php do_action('back_button'); ?>
Let me know.

add_shortcode( 'ekaa_back_btn' , 'ekaaBackButton');
function ekaaBackButton(){
if ( wp_get_referer() ) {
$button = "\n<button id='ekaa-back-button' class='btn button ekaa-back-button' onclick='javascript:history.back()'><i class='fas fa-arrow-left'></i>Back</button>";
echo ( $button );
}
}
this is work for me using WordPress Shortcode

Related

Looking for query to get meta_value

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
);
}

Adding custom button next to "Add to Cart" Button" Woocommerce

Hi guys im trying to add custom button next to "Add to Cart" button. I added it but i want to change button link for every product how can i do that here is my code: ( i took from another question, it works )
add_action('woocommerce_after_shop_loop_item', 'add_a_custom_button', 5 );
function add_a_custom_button() {
global $product;
// Not for variable and grouped products that doesn't have an "add to cart" button
if( $product->is_type('variable') || $product->is_type('grouped') ) return;
// Output the custom button linked to the product
echo '<div style="margin-bottom:10px;">
<a class="button custom-button" href="' . esc_attr( $product->get_permalink() ) . '">' . __('View product') . '</a>
</div>';
}
Another way:
Create a new Wordpress custom field for the pages you want to use a custom url link on:
Custom field name: nothanks_link_redirect
Custom field value: https://yourlink.com
Tweak as you need and put this in your child theme's functions.php :
/** WooCommerce custom field - 'No Thanks' Button **/
function nothanks_redirect_button() {
global $post;
$product_id = $post->ID;
$NoThanksLinkRedirectValue = get_post_meta($product_id,'nothanks_link_redirect',true);
if(!$NoThanksLinkRedirectValue) return;
echo '<a class="nothanks-button" style="margin-left: 20px" href="'.$NoThanksLinkRedirectValue.'" target="_self">No Thanks</a>';
}
add_action('woocommerce_after_add_to_cart_button','nothanks_redirect_button');
Enjoy!

Allow one cart item before place order button click or proceed to checkout button click

I want to disallow place order if cart item is more than two on click of proceed to checkout or Place Order button click in woocommerce.
I do not want to check it on add to cart validation check, please any one can guide me about it?
You can use WooCommerce Min/Max Quantities extension to set a minimum and maximum quantity required to checkout.
or
You can set custom code to your functions.php theme file by using "woocommerce_after_checkout_validation" filter and can use $posted array to check value and set validation,
add_action('woocommerce_after_checkout_validation', 'rei_after_checkout_validation');
function rei_after_checkout_validation( $posted ) {
// do all your logics here...
}
function custom_checkout_button_action(){
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$total = 0;
foreach($items as $item => $values) {
$total = $values['quantity'];
}
if($total>=2){
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
else{
?>
<?php _e( 'Check On Out', 'woocommerce' ); ?>
<?php
}
}
add_action('woocommerce_proceed_to_checkout', 'custom_checkout_button_action');
If you are using butto you can change your HTML accordingly to disable button instead of link in above code on check out page before place an order page.

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.

create custom field and upload image in wordpress

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] );

Resources