admin管理员组文章数量:1303449
This function allows me to show Demo Url on the single product page before cart button. But i want to display it somewhere else using a shortcode. Please help me How to do that-
Here is the code:
/**
* Show a Live Demo button on a single product page
*/
function isa_before_add_to_cart_form() {
global $product;
$url = get_post_meta( $product->get_id(), '_isa_wc_product_demo_url', true );
if ( $url ) {
echo '<p><a href="' . esc_url( $url ) . '" id="livedemo" class="button primary is-shade box-shadow-3 box-shadow-5-hover" target="_blank" rel="noopener">' .
__( 'Live Demo', 'textdomain') .
'</a><p>';
}
}
add_action('woocommerce_before_add_to_cart_form','isa_before_add_to_cart_form');
/**
* Display the Demo URL field in the Product Data metabox
*/
function isa_wc_product_add_demo_url_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_isa_wc_product_demo_url',
'label' => __( 'Demo URL', 'textdomain' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Set a URL that will be displayed on the product page to link to a demo of this product. Full URL starting with "https://"', 'textdomain' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'isa_wc_product_add_demo_url_field' );
/**
* Save the Demo URL field value when the produc is saved
*/
function isa_wc_product_save_demo_url_value( $post_id ) {
$val = trim( get_post_meta( $post_id, '_isa_wc_product_demo_url', true ) );
$new = sanitize_text_field( $_POST['_isa_wc_product_demo_url'] );
if ( $val != $new ) {
update_post_meta( $post_id, '_isa_wc_product_demo_url', $new );
}
}
add_action( 'woocommerce_process_product_meta', 'isa_wc_product_save_demo_url_value' );
This function allows me to show Demo Url on the single product page before cart button. But i want to display it somewhere else using a shortcode. Please help me How to do that-
Here is the code:
/**
* Show a Live Demo button on a single product page
*/
function isa_before_add_to_cart_form() {
global $product;
$url = get_post_meta( $product->get_id(), '_isa_wc_product_demo_url', true );
if ( $url ) {
echo '<p><a href="' . esc_url( $url ) . '" id="livedemo" class="button primary is-shade box-shadow-3 box-shadow-5-hover" target="_blank" rel="noopener">' .
__( 'Live Demo', 'textdomain') .
'</a><p>';
}
}
add_action('woocommerce_before_add_to_cart_form','isa_before_add_to_cart_form');
/**
* Display the Demo URL field in the Product Data metabox
*/
function isa_wc_product_add_demo_url_field() {
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_isa_wc_product_demo_url',
'label' => __( 'Demo URL', 'textdomain' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Set a URL that will be displayed on the product page to link to a demo of this product. Full URL starting with "https://"', 'textdomain' )
)
);
echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'isa_wc_product_add_demo_url_field' );
/**
* Save the Demo URL field value when the produc is saved
*/
function isa_wc_product_save_demo_url_value( $post_id ) {
$val = trim( get_post_meta( $post_id, '_isa_wc_product_demo_url', true ) );
$new = sanitize_text_field( $_POST['_isa_wc_product_demo_url'] );
if ( $val != $new ) {
update_post_meta( $post_id, '_isa_wc_product_demo_url', $new );
}
}
add_action( 'woocommerce_process_product_meta', 'isa_wc_product_save_demo_url_value' );
Share
Improve this question
asked Feb 9, 2021 at 18:25
Mewaram kansodiyaMewaram kansodiya
134 bronze badges
1 Answer
Reset to default -1You can use the add_shortcode function to do that. You just need to pass your isa_before_add_to_cart_form
function as a shortcode like this:
add_shortcode( 'isa_show_link', 'isa_before_add_to_cart_form );
Now you can use the shortcode [isa_show_link]
to display it anywhere. It's simple.
本文标签: pluginsHow to Create a shortcode to this php function
版权声明:本文标题:plugins - How to Create a shortcode to this php function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741760470a2396373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论