简体   繁体   English

Jquery wordpress 插件 function

[英]Jquery wordpress plugin function

I make my own plugin but I have a problem when I try to pass my function with the button.我制作了自己的插件,但是当我尝试使用按钮传递 function 时出现问题。 When I reload the page, my function passes but when I press the button, the function doesn't work.当我重新加载页面时,我的 function 会通过,但是当我按下按钮时,function 不起作用。 error Generate_coupon_woo undefined.错误 Generate_coupon_woo 未定义。 I tried to do this for when a user tried to generate code with a click.当用户尝试通过单击生成代码时,我尝试这样做。

I make my own plugin but I have a problem when I try to pass my function with the button.我制作了自己的插件,但是当我尝试使用按钮传递 function 时出现问题。 When I reload the page, my function passes but when I press the button, the function doesn't work.当我重新加载页面时,我的 function 会通过,但是当我按下按钮时,function 不起作用。 error Generate_coupon_woo undefined.错误 Generate_coupon_woo 未定义。 I tried to do this for when a user tried to generate code with a click.当用户尝试通过单击生成代码时,我尝试这样做。

<?php
/**
 * Plugin Name: Demo remote post
 * Description: connectd
 * Version:     1.0.0
 * Author:      Digital Studio
 */
defined('ABSPATH') or die('Unauthorized Access !');



add_shortcode('nouveauShortcode', 'gererShortcode');




function gererShortcode(){
  ?>
 
  <div class="wrap">
    <h2>Digital Studio Plugin</h2>
    <button class="btn btn-primary" type="but" id="submit">code Promo</button>
    </div>
    <script>
      jQuery(document).ready(function($) {
        $("#submit").on('click', Generate_coupon_woo);
      });
    </script>
  <?php
  } 
  






    function Generate_coupon_woo(){
        $url='localhost:3306';
     
        $coupon_code = substr( "abcdefghijklmnopqrstuvwxyz123456789", mt_rand(0, 50) , 1) .substr( md5( time() ), 1); // Code
        $coupon_code = substr( $coupon_code, 0,10); // create 10 letters coupon
        $amount = '15'; // Amount
        $discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product // ici le type soit fixé sur le panier à la fin, soit en pourcentage sur le panier , soit fixé sur le produit, soit en pourcentage sur le produit
        
        $coupon = array(
            'post_title'   => $coupon_code,
            'post_content' => '€15 off coupon',
            'post_excerpt' => '€15 off coupon',
            'post_status'  => 'publish',
            'post_author'  => 1,
            'post_type'    => 'shop_coupon'
        );
     
     
        $username = "*******";
        $password = "********";
        $database = "******";
        
        $mydb = new wpdb($username, $password, $database, $url);
        $rows = $mydb->get_results("SELECT comment_author FROM VWfBb8z7_comments");
        //$new_coupon_id = $mydb->wp_insert_post( $coupon );
        $insert_test = $mydb->insert( 'VWfBb8z7_posts', $coupon );
          
        update_post_meta( $insert_test, 'discount_type', $discount_type );
        update_post_meta( $insert_test, 'coupon_amount', $amount );
        update_post_meta( $insert_test, 'individual_use', 'yes' );
        update_post_meta( $insert_test, 'product_ids', '' );
        update_post_meta( $insert_test, 'exclude_product_ids', '' );
        update_post_meta( $insert_test, 'usage_limit', '1' );
        update_post_meta( $insert_test, 'expiry_date', '' );
        update_post_meta( $insert_test, 'apply_before_tax', 'no' );
        update_post_meta( $insert_test, 'free_shipping', 'no' );      
        update_post_meta( $insert_test, 'exclude_sale_items', 'no' );     
        update_post_meta( $insert_test, 'free_shipping', 'no' );      
        update_post_meta( $insert_test, 'product_categories', '' );       
        update_post_meta( $insert_test, 'exclude_product_categories', '' );       
        update_post_meta( $insert_test, 'minimum_amount', '' );       
        update_post_meta( $insert_test, 'customer_email', '' ); 
      }
      

  ?>

According to the documentation for add_shortcode: https://developer.wordpress.org/reference/functions/add_shortcode/根据 add_shortcode 的文档: https://developer.wordpress.org/reference/functions/add_shortcode/

"Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call since you cannot control when and where they are called from." “请注意,由简码调用的 function 不应产生任何类型的 output。简码函数应返回用于替换简码的文本。产生 Z78E6221F6393D1356DZF 的方式与直接导致意外结果的方式相似。14CE681DB39过滤器函数的行为应该是,它们不应从调用中产生预期的副作用,因为您无法控制调用它们的时间和地点。”

You may want to change your strategy on how you want to implement this code, and where you want it to be implemented.您可能希望更改有关如何实现此代码以及在何处实现它的策略。

If you do want to go this route.如果您确实想要 go 这条路线。 Try adding this.尝试添加这个。 Adding to init gives it time for WordPress to initialise it.添加到 init 可以让 WordPress 有时间对其进行初始化。

add_action( 'init', 'wpdocs_add_custom_shortcode' );
 
function gerer_add_custom_shortcode() {
    add_shortcode('nouveauShortcode', 'gererShortcode');
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM