Monday, October 3, 2022
HomeWordPress Developmenttypes - Including Mailchimp API key dynamically

types – Including Mailchimp API key dynamically


Good day everybody i’m making a customized plugin and i’m connecting it to the mailchimp api it is all working however i’ve one thing to alter i need to add the api key or record id within the admin facet which suggests there’s a kind within the admin facet which so as to add the api key and record id
i dont need to add it within the code. right here is the total code i simply mixed this code that i obtained on the web. credit to them. i dont know if i do it appropriately however TIA for the assistance have a very good day!

<?php

/*
Plugin Identify: Kind API

Description: Easy kind with api
Model: 1.0
Creator:

*/

if( !outlined(‘ABSPATH’) )
{

exit;

}

// plugin activation hook

register_activation_hook(FILE, ‘mytable_activation_function’);

// callback perform to create desk

//if activate create a database

perform mytable_activation_function()
{
world $wpdb;

if ($wpdb->get_var("present tables like '" . create_my_table() . "'") != create_my_table()) {

    $mytable="CREATE TABLE `" . create_my_table() . '` (
                        `form_id` int(11) NOT NULL AUTO_INCREMENT,
                        `api_key` varchar(150) NOT NULL,
                        `list_id` varchar(150) NOT NULL,
                        `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
                        PRIMARY KEY (`form_id`)
                      ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;';
    require_once(ABSPATH . 'wp-admin/consists of/improve.php');
    dbDelta($mytable);
}

}
// returns desk identify
perform create_my_table()
{
world $wpdb;
return $wpdb->prefix . “custom_api”;
}

perform actions_add_form_list() {

//that is the shape within the admin facet which is able to hook up with the shape within the entrance finish and in right here the api and record id can be added
echo '<kind motion="" technique="submit">';
echo '<p>';
echo '<h2>Add your API key heresss and record id</h2> <br/>';
echo '<enter kind="textual content" identify="api_key" placeholder="api key"autocomplete="off""  worth="' . ( isset( $_POST["api_key"] ) ? esc_attr( $_POST["api_key"] ) : '' ) . '" dimension="40" />';
echo '</p>';
echo '<p><enter kind="textual content" identify="list_id" placeholder="record id"autocomplete="off""  worth="' . ( isset( $_POST["list_id"] ) ? esc_attr( $_POST["list_id"] ) : '' ) . '" dimension="40" /></p>';
echo '<p><enter kind="submit" identify="add-key" worth="Submit key"></p>';
echo '</kind>';

//perform for the admin kind

perform test_plugin_capture() {

if (isset( $_POST['add-key'] ) ) {

    $knowledge = array(
        $api_key => $_POST['api_key'],
        $list_id => $_POST['list_id']
    );

    $table_name="wp_custom_api";
    $end result = $wpdb->insert($table_name, $knowledge, $format=NULL);

    if($end result == 1) {
        echo 'efficiently inserted!';
    }else {
        echo 'error!';
    }
}

}
}
//perform to show the api key and record id

perform action_display_key_lists() {
embrace(‘show.php’);
}
perform actions_recent_bids_add_admin_page(){

add_menu_page(
      'API key',
      'API',
      'manage_options',
      'wc-auction-reports',
      'actions_add_form_list',
      'dashicons-rest-api',
       56
);

add_submenu_page(
      'wc-auction-reports',               // dad or mum slug
      'Listing of key',                // web page title
      'Listing of key',                // menu title
      'manage_options',                   // functionality
      'wc-acutions-list-keys',  // slug
      'action_display_key_lists' // callback
);

}

add_action(‘admin_menu’,’actions_recent_bids_add_admin_page’);

perform html_form_code() {
echo ”;
echo ‘

‘;
echo ‘Identify (required)
‘;
echo ‘<enter kind=”textual content” identify=”form_name” autocomplete=”off” sample=”[a-zA-Z0-9 ]+” required worth=”‘ . ( isset( $_POST[“form_name”] ) ? esc_attr( $_POST[“form_name”] ) : ” ) . ‘” dimension=”40″ />’;
echo ‘

‘;
echo ‘

‘;
echo ‘E mail (required)
‘;
echo ‘<enter kind=”e mail” identify=”form_email” autocomplete=”off” required worth=”‘ . ( isset( $_POST[“form_email”] ) ? esc_attr( $_POST[“form_email”] ) : ” ) . ‘” dimension=”40″ />’;
echo ‘

‘;
echo ‘

‘;
echo ‘Telephone (required)
‘;
echo ‘<enter kind=”textual content” identify=”form_phone” autocomplete=”off” required’ . ( isset( $_POST[“form_phone”] ) ? esc_attr( $_POST[“form_phone”] ) : ” ) . ‘” dimension=”40″ />’;
echo ‘

‘;
echo ”;
echo ”;
}

perform deliver_mail() {

// if the submit button is clicked, add to subscriber
if ( isset( $_POST['cf-submitted'] ) ) {
 // let's begin with some variables
  $form_name = $_POST['form_name'];// the person identify we're going to subscribe
  $form_email = $_POST['form_email'] ;// the person e mail we're going to subscribe
  $form_phone = $_POST['form_phone'];// the person telephone we're going to subscribe


    //lets name the api and record id within the database

    $apiKey = 'i dont need to add right here the api key';
    $list_id = ''; // Listing / Viewers ID
   $server = explode( '-', $apiKey );


  $url="https://" . $server[1] . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/';

  $response = wp_remote_post(
$url,
[
    'method'      => 'POST',
    'data_format' => 'body',
    'timeout'     => 45,
    'headers'     => [
        'Authorization' => 'apikey ' . $apiKey,
        'Content-Type'  => 'application/json; charset=utf-8',
    ],
    'physique'        => json_encode(
        [
            'email_address' => $form_email,//email
            'status'        => 'subscribed',
            'status_if_new' => 'subscribed',
            'merge_fields'  => [
                'FNAME' => $form_name,// first name
               
                'PHONE' => $form_phone//phone
            ]
        ]
    )
]

);
if( ‘OK’ === wp_remote_retrieve_response_message( $response ) ) {
echo ‘Nice you’ve gotten efficiently subscribed’;
}

}

}

perform api_shortcode() {
ob_start();
deliver_mail();
html_form_code();

return ob_get_clean();

}

add_shortcode( ‘api_form’, ‘shortcode’ );

?>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments