Support » Plugins » Hacks » How to get Values from FrontEnd User Form and display in WP Admin Panel?

  • aleykey

    (@aleykey)


    Hello guys,

    Iam tryring to get Values from a Form which i have included in the FronEnd (RadioButton Value and Text Input Value) to display in the WordPress
    BackEnd or AdminPanel, so I need to have a Admin Menu to get the Values, a specific user (E-Mail Adress) entered in the Form and display this Values in the Backend.

    I am using a Newsletter Plugin its called MyMail for WordPress. I want to know why Users unsubscribe to my Newsletter by letting them check Radio Buttons with Reasons or write a Text in a Textarea input.
    I modified the unsubscribe form and added some radio buttons and a textarea heres the code:

    ………..

    $html = ”;

    $html .= $this->get_styles();

    $action = ‘mymail_form_unsubscribe’;

    $html .= ‘<form action=”‘.$this->get_form_action($action).'” method=”post” class=”mymail-form mymail-form-submit mymail-ajax-form” id=”mymail-form-unsubscribe”>’.”\n”;
    $html .= ‘<div class=”mymail-form-info ‘.$infoclass.'”>’;

    $html .= $this->get_message();
    $html .= $this->message;
    $html .= ‘</div>’;
    $html .= ‘<input name=”hash” type=”hidden” value=”‘.$this->hash.'”>’;
    $html .= ‘<input name=”campaign” type=”hidden” value=”‘.$this->campaignID.'”>’;

    if(!$this->hash){

    $html .= ‘<div class=”mymail-wrapper mymail-email-wrapper”><div class=”mymail-form-fields”>
    <div class=”mymail-form-info-checkbox”>
    <fieldset id=”gruende”>

    • <label> <input id=”one” class=”.myCheckbox” type=”radio” name=”reason” value=”notwanted”> Ich habe mich nie für diesen Newsletter angemeldet </label>
    • <label> <input id=”two” class=”.myCheckbox” type=”radio” name=”reason” value=”notrelevant”> Die im Newsletter genannten Themen sind für mich nicht relevant </label>
    • <label> <input id=”three” class=”.myCheckbox” type=”radio” name=”reason” value=”notinterested”> Ich erhalte zu viele Newsletter </label>
    • <label> <input id=”four” class=”.myCheckbox” type=”radio” name=”reason” value=”often”> Ich bin an der Zeitarbeitsbranche nicht mehr interessiert </label>
    • <label> <input id=”otherreasons” type=”radio” name=”reason” value=”otherreason”> Sonstiges: </label>
    • <label> <input style=”height: 125px; width: 80%;” id=”otherreasontextfield” type=”text” name=”ownreason” value=”” placeholder=”Bitte geben Sie einen Grund an”></label>

    </fieldset>
    </div><div id=”emailproof”><label for=”mymail-email”>’.mymail_text(’email’, __(‘Email’, ‘mymail’)).’ <span class=”mymail-required”>*</span></label>’;
    $html .= ‘<input id=”mymail-email” class=”input mymail-email mymail-required” name=”email” type=”email” value=””>’;

    }
    if($subscriber && $single_opt_out){
    }else{
    $buttontext = mymail_text(‘unsubscribebutton’, __(‘Unsubscribe’, ‘mymail’));
    $html .= ‘<div class=”mymail-wrapper mymail-submit-wrapper form-submit”><input name=”submit” type=”submit” value=”‘.$buttontext.'” class=”submit-button button”></div>’;
    $html .= ‘</div></div>’;
    }
    $html .= ‘</form>’;

    return apply_filters(‘mymail_unsubscribe_form’, $html, $this->campaignID);
    }

    …………..

    Hope somebody can help me with this.
    Best Regards.

    AleyKey

Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    First you need to go to the code that executes when the form is submitted. Unless AJAX is used, this will be where ever the form tag’s action attribute points to. In your code this is the value returned by $this->get_form_action($action). You’ll need to insert code there to save your added field data somewhere in the DB.

    On the backend, use the Admin Menu API (see Administration Menus) to add a menu item and content to display the saved values.

    aleykey

    (@aleykey)

    Thank you very much this was helpful,

    but the Plugin is a little bit difficult to customize I think I found this in the same code file, it´s submitted via ajax but can´t help it out.

    public function unsubscribe( ) {

    $return[‘success’] = false;

    $_BASE = $_POST;

    if(empty($_BASE)){
    wp_die(‘no data’);
    };

    $campaign_id = !empty($_BASE[‘campaign’]) ? intval($_BASE[‘campaign’]) : NULL;

    if(isset($_BASE[’email’])){
    $return[‘success’] = mymail(‘subscribers’)->unsubscribe_by_mail($_BASE[’email’], $campaign_id);
    }else if(isset($_BASE[‘hash’])){
    $return[‘success’] = mymail(‘subscribers’)->unsubscribe_by_hash($_BASE[‘hash’], $campaign_id);
    }else{
    //wp_redirect(mymail()->get_unsubscribe_link());
    //exit;
    }

    //redirect if no ajax request
    if (isset($_SERVER[‘HTTP_X_REQUESTED_WITH’])) {

    $return[‘html’] = $return[‘success’]
    ? mymail_text(‘unsubscribe’)
    : (empty($_POST[’email’])
    ? mymail_text(‘enter_email’)
    : mymail_text(‘unsubscribeerror’));

    @header( ‘Content-type: application/json’ );
    echo json_encode($return);
    exit;

    }else{

    if($return[‘success’]){
    wp_die($return[‘html’].’‘.mymail_text(‘unsubscribe’).’‘);
    }else{
    wp_die($return[‘html’].’‘.(empty($_POST[’email’]) ? mymail_text(‘enter_email’) : mymail_text(‘unsubscribeerror’)).’‘);
    }
    exit;

    }

    }

    public function strip_css($css) {
    $css = strip_tags($css);
    $css = preg_replace(‘!/\*[^*]*\*+([^/][^*]*\*+)*/!’, ”, $css);
    $css = trim(str_replace(array(“\r\n”, “\r”, “\n”, “\t”, ‘ ‘, ‘ ‘), ”, $css));
    $css = str_replace(‘ {‘, ‘{‘, $css);
    $css = str_replace(‘{ ‘, ‘{‘, $css);
    $css = str_replace(‘ }’, ‘}’, $css);
    $css = str_replace(‘}’, ‘}’.”\n”, $css);
    return $css;
    }

    private function get_form_action($action = ”) {

    $is_permalink = mymail(‘helper’)->using_permalinks();

    $prefix = !mymail_option(‘got_url_rewrite’) ? ‘/index.php’ : ‘/’;

    return $is_permalink
    ? home_url($prefix.’/mymail/’.$this->form_endpoint)
    : add_query_arg(array(‘action’ => $action), admin_url(‘admin-ajax.php’, $this->scheme));

    }

    Jerry

    (@phyrax)

    @aleykey – That unsubscribe() function is part of a hook. Look into how AJAX works with WordPress here: https://codex.wordpress.org/AJAX_in_Plugins

    There has to be an action similar to add_action( 'wp_ajax_{ajax_action}', array( $this, 'unsubscribe' ) ); unless it wasn’t inside of a class then the last parameter wouldn’t be an array.

    So to the point of @bcworkz – once you have the wp_ajax_{ajax_action} name, you can simply make your own, as well as your own function, and handle the data like you want to. You won’t have to edit the plugin’s file in any way.

    aleykey

    (@aleykey)

    Hey thank you very much. That helped me very much .But I can´t find wp_ajax_{ajax_action}…. for something with unsubscribe. The PlugIn has many Directories with many Files for example in classes directory there are ajax.class.php and actions.class.php etc. but I can´t figure it out so I can´t find the actions name to call it.

    I´m Stuck with this.

    Best Regards

    aleykey

    (@aleykey)

    I found it

    add_action(‘mymail_unsubscribe’, array( &$this, ‘unsubscribe’), 10 , 2);

    Know how do I get the values out of it and add it to the WP Admin Panel as a Menu or Tab , for each unsubscribed user Like a table or something else

    aleykey

    (@aleykey)

    But I´am not quite sure I found it in the action.class.php

    <?php if(!defined(‘ABSPATH’)) die(‘not allowed’);

    class mymail_actions {

    // 1 2 3 4 5 6 7
    private $types = array(”, ‘sent’, ‘open’, ‘click’, ‘unsubscribe’, ‘bounce’, ‘hardbounce’, ‘error’);

    public function __construct() {

    add_action(‘plugins_loaded’, array( &$this, ‘init’ ), 1 );

    }

    public function init() {

    add_action(‘mymail_send’, array( &$this, ‘send’), 10 , 2);
    add_action(‘mymail_open’, array( &$this, ‘open’), 10 , 3);
    add_action(‘mymail_click’, array( &$this, ‘click’), 10 , 4);
    add_action(‘mymail_unsubscribe’, array( &$this, ‘unsubscribe’), 10 , 2);
    add_action(‘mymail_bounce’, array( &$this, ‘bounce’), 10 , 3);
    add_action(‘mymail_subscriber_error’, array( &$this, ‘error’), 10 , 3);

    add_action(‘mymail_cron’, array( &$this, ‘cleanup’));

    }

    public function get_fields($fields = NULL, $where = NULL) {

    global $wpdb;

    $fields = esc_sql(is_null($fields) ? ‘*’ : (is_array($fields) ? implode(‘, ‘, $fields) : $fields));

    $sql = “SELECT $fields FROM {$wpdb->prefix}mymail_actions WHERE 1=1”;
    if(is_array($where)){
    foreach($where as $key => $value){
    $sql .= “, “.esc_sql($key).” = ‘”.esc_sql($value).”‘”;
    }
    }

    return $wpdb->get_results($sql);

    }

    public function send($subscriber_id, $campaign_id) {

    return $this->add_action(array(
    ‘subscriber_id’ => $subscriber_id,
    ‘campaign_id’ => $campaign_id,
    ‘type’ => 1,
    ), true);

    }

    public function open($subscriber_id, $campaign_id, $explicit = true) {

    return $this->add_subscriber_action(array(
    ‘subscriber_id’ => $subscriber_id,
    ‘campaign_id’ => $campaign_id,
    ‘type’ => 2,
    ), $explicit);

    }

    public function click($subscriber_id, $campaign_id, $link, $index = 0, $explicit = true) {

    $this->open($subscriber_id, $campaign_id, false);

    $link_id = $this->get_link_id($link, $index);

    return $this->add_subscriber_action(array(
    ‘subscriber_id’ => $subscriber_id,
    ‘campaign_id’ => $campaign_id,
    ‘type’ => 3,
    ‘link_id’ => $link_id,
    ), $explicit);

    }

    public function unsubscribe($subscriber_id, $campaign_id) {

    return $this->add_action(array(
    ‘subscriber_id’ => $subscriber_id,
    ‘campaign_id’ => $campaign_id,
    ‘type’ => 4,
    ));

    }

    Moderator bcworkz

    (@bcworkz)

    Sorry, that unsubscribe appears to be for something else. The AJAX action tag you’re looking for needs to begin with ‘wp_ajax_`. There may not be much to gain by finding this anyway, it’s unlikely to have a usable hook, and you would still need to alter the initiating JS script anyway (to send the added data). Since you have to modify the JS script, you may as well initiate your own AJAX call before the current call. Making two calls is not ideal, but hacking plugin code is hardly ideal either.

    A good approach would be to change the JS function called when the form is submitted to your own custom version that first sends your added data via AJAX. Your AJAX handler saves the data somewhere. Your JS script continues by calling the function that was originally called upon form submit.

    Then, as I first suggested, you could add an admin page that displays the data from where ever it is stored.

    Hey @bcworkz I managed to get the values out of the form!
    This is what the console responds
    Parameterapplication/x-www-form-urlencodedNicht sortieren
    campaign
    0
    email
    testmail@live.de
    hash
    ownreason
    qwgzdru
    reason
    otherreason
    Quelle
    hash=&campaign=0&reason=otherreason&ownreason=qwgzdru&email=testmail%40live.de

    Now how can I put the values which are shown in the console after submitting the form, in a wordpress Admin menu page? or a table?

    Thank you very much.

    Best regards

    Moderator bcworkz

    (@bcworkz)

    Send the data back to the sever using AJAX techniques. Jerry provided one link above. Another useful reference is https://developer.wordpress.org/plugins/javascript/jquery/

    Your JS or jQuery script also needs to send an ‘action’ value which is used by WP to construct an action hook. For example, if you set action: 'aleykey' with JS (along with all other form data you want to send), then the action hook will be ‘wp_ajax_aleykey’. The PHP function you create to save the form data to a table is added as an action callback for that hook.

    A good place to store your data might be the usermeta table. There should be something in the form data that allows you to determine the user’s ID, such as their e-mail. Once the user ID is determined, assemble the data into a single array, then use update_user_meta() to store the data array.

    To display the data to an admin user, create a menu page for that purpose with add_menu_page(). You need to supply a callback function name as a parameter. The callback is called when it is appropriate to output page content. There’s a number of ways to get the data to be displayed. I suggest using $wpdb methods to directly query the DB for all records in usermeta having the meta key you used to store the data with the AJAX handler function.

    You admin menu page function can then loop through all the records returned and output the data in some useful format, a table perhaps. It’d be cool to use the same table structure like the one used for post and page lists. There is a learning curve for using the WP_List_Table class though, so I suggest you forgo that option for now and simply output a conventional HTML table.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to get Values from FrontEnd User Form and display in WP Admin Panel?’ is closed to new replies.