简体   繁体   English

PHP Function 内 Wordpress 简码

[英]PHP Function inside Wordpress Shortcode

I have a wordpress plugin that allows users to upload avatars to my website.我有一个 wordpress 插件,允许用户将头像上传到我的网站。 This plugin contains a single shortcode that combines thumbnail, upload and update buttons in one layout.此插件包含一个简码,将缩略图、上传和更新按钮组合在一个布局中。

What I'm trying to do is put the three elements (thumbnail, upload and update buttons) in three different shortcodes.我要做的是将三个元素(缩略图、上传和更新按钮)放在三个不同的简码中。 This way I can place elements anywhere on the page.这样我就可以将元素放置在页面的任何位置。

For the thumbnail I have already succeeded with the help of a stack user: How to put part of php code into wordpress shortcode对于缩略图,我已经在堆栈用户的帮助下成功: 如何将 php 代码的一部分放入 wordpress 简码

Now I would like to do the same thing but with update and upload buttons, I get the elements inside the shortcode but they don't work.现在我想做同样的事情,但使用更新和上传按钮,我得到了短代码中的元素,但它们不起作用。 They are just plain non-clickable buttons, can someone help me figure out where am I wrong?它们只是普通的不可点击按钮,有人可以帮我找出我错在哪里吗?

For the first shortcode I referenced this plugin file: https://github.com/onedesigns/one-user-avatar/blob/main/includes/class-wp-user-avatar-shortcode.php#L305对于第一个简码,我引用了这个插件文件: https://github.com/onedesigns/one-user-avatar/blob/main/includes/class-wp-user-avatar-shortcode.php#L305

//SHORTCODE UPDATE BUTTON ONE-USER-AVATAR
function demo1_shortcode() {
    
// Global
global $current_user, $wpua_functions,  $wpua_edit_avatar, $wpua_default_avatar_updated, $wpua_users_updated, $wpua_media_updated;

// Current user
$user = $current_user;
        
// Function ??   

// Update Button
?>
<?php submit_button( __( 'Update Profile', 'one-user-avatar' ) ); ?>
<?php

}
add_shortcode('demo1', 'demo1_shortcode'); 

For the second shortcode I referenced this other plugin file: https://github.com/onedesigns/one-user-avatar/blob/main/includes/class-wp-user-avatar.php#L253对于第二个短代码,我引用了另一个插件文件: https://github.com/onedesigns/one-user-avatar/blob/main/includes/class-wp-user-avatar.php#L253

//SHORTCODE UPLOAD BUTTON ONE-USER-AVATAR
function demo2_shortcode() {
    
// Global
global $current_user, $wpua_functions,  $wpua_edit_avatar, $wpua_allow_upload, $wpua_force_file_uploader;

// Current user
$user = $current_user;
        
// Function ??  
  
// Upload Button PHP

?>
 <p id="<?php echo esc_attr( ( 'add-new-user' == $user ) ? 'wpua-upload-button' : 'wpua-upload-button-existing' ); ?>">
  <input name="wpua-file" id="<?php echo esc_attr( ( 'add-new-user' == $user ) ? 'wpua-file' : 'wpua-file-existing' ); ?>" type="file" />
   <button type="submit" class="button" id="<?php echo esc_attr( ( 'add-new-user' == $user ) ? 'wpua-upload' : 'wpua-upload-existing' ); ?>" name="submit" value="<?php esc_html_e( 'Upload', 'one-user-avatar' ); ?>">
<?php esc_html_e( 'Upload', 'one-user-avatar' ); ?>
   </button>
 </p>   
<?php       

}
add_shortcode('demo2', 'demo2_shortcode');

The buttons appear but do not perform any function.按钮出现但不执行任何 function。 I believe this happens because I didn't include the functions in the shortcode?我相信这是因为我没有在短代码中包含这些功能? But I don't know, I'm a newbie, sorry for that.但是我不知道,我是新手,对不起。

As I understand you build HTML form for submission on frontend page using your shortcodes.据我了解,您使用简码构建 HTML 表单以在前端页面上提交。 Your form ( form tag) should have ACTION that you can catch in your plugin functions.您的表单(表单标签)应该具有可以在插件函数中捕获的 ACTION。 For example if you will have form action like CURRENT_PAGE_URL?act=someslug_upload_avatar you can catch this in plugin code (in some main function that executed on any frontend page load):例如,如果您将有像CURRENT_PAGE_URL?act=someslug_upload_avatar这样的表单操作,您可以在插件代码中捕捉到这一点(在任何前端页面加载时执行的一些主要 function 中):

if(!empty($_REQUEST['act']) && $_REQUEST['act'] == 'someslug_upload_avatar') {
   // Work with your form fields data from $_REQUEST and upload avatar via your existing functions in plugin.
}

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

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