mabyabt's blog

WordPress vs drupal module development comparison

Sure, I can simplify the guide and make it more accessible for a blog format, specifically for Bearblog. Bearblog is known for its minimalist approach, so we’ll keep the guide concise and to the point.


How to Create and Configure API Integration Modules for Drupal and WordPress

Integrate Foxpost, MPL, and GLS APIs into your Drupal and WordPress sites with these easy-to-follow guides.


Drupal Module: Foxpost, MPL, and GLS Integration

1. Set Up Your Drupal Module

  1. Create Directory Structure:

    • Navigate to modules/custom in your Drupal installation.
    • Create a folder named foxpost.
  2. Add Files:

    • foxpost.info.yml (in foxpost/):

      name: 'Foxpost Integration'
      type: module
      description: 'Integrates Foxpost, MPL, and GLS APIs.'
      core_version_requirement: ^8 || ^9
      dependencies:
        - drupal:config
      
    • src/Service/ApiService.php (in foxpost/src/Service/):

      <?php
      
      namespace Drupal\foxpost\Service;
      
      use GuzzleHttp\ClientInterface;
      
      class ApiService {
        protected $httpClient;
      
        public function __construct(ClientInterface $http_client) {
          $this->httpClient = $http_client;
        }
      
        public function fetchData($endpoint, $params) {
          $response = $this->httpClient->get($endpoint, ['query' => $params]);
          return json_decode($response->getBody(), TRUE);
        }
      }
      
    • src/Form/FoxpostSettingsForm.php (in foxpost/src/Form/):

      <?php
      
      namespace Drupal\foxpost\Form;
      
      use Drupal\Core\Form\ConfigFormBase;
      use Drupal\Core\Form\FormStateInterface;
      
      class FoxpostSettingsForm extends ConfigFormBase {
        public function getFormId() {
          return 'foxpost_settings_form';
        }
      
        protected function getEditableConfigNames() {
          return ['foxpost.settings'];
        }
      
        public function buildForm(array $form, FormStateInterface $form_state) {
          $config = $this->config('foxpost.settings');
          $form['foxpost_api_key'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Foxpost API Key'),
            '#default_value' => $config->get('foxpost_api_key'),
          ];
          $form['mpl_api_key'] = [
            '#type' => 'textfield',
            '#title' => $this->t('MPL API Key'),
            '#default_value' => $config->get('mpl_api_key'),
          ];
          $form['gls_api_key'] = [
            '#type' => 'textfield',
            '#title' => $this->t('GLS API Key'),
            '#default_value' => $config->get('gls_api_key'),
          ];
          return parent::buildForm($form, $form_state);
        }
      
        public function submitForm(array &$form, FormStateInterface $form_state) {
          $this->config('foxpost.settings')
            ->set('foxpost_api_key', $form_state->getValue('foxpost_api_key'))
            ->set('mpl_api_key', $form_state->getValue('mpl_api_key'))
            ->set('gls_api_key', $form_state->getValue('gls_api_key'))
            ->save();
          parent::submitForm($form, $form_state);
        }
      }
      
    • foxpost.module (in foxpost/):

      <?php
      
      function foxpost_menu() {
        $items = [];
        $items['foxpost/locations'] = [
          'title' => 'Foxpost Locations',
          'page callback' => 'foxpost_location_page',
          'access callback' => TRUE,
        ];
        return $items;
      }
      
      function foxpost_location_page() {
        $api_service = \Drupal::service('foxpost.api_service');
        $data = $api_service->fetchData('https://api.foxpost.example/locations', []);
        return ['#markup' => '<pre>' . print_r($data, TRUE) . '</pre>'];
      }
      

2. Upload and Install the Module

  1. Upload:

    • Use FTP/SFTP to upload foxpost to modules/custom.
  2. Install:

    • Navigate to Admin > Extend and enable the Foxpost module.
  3. Configure:

    • Go to Admin > Configuration > Web services > Foxpost Settings.
    • Enter API credentials for Foxpost, MPL, and GLS.
  4. Test:

    • Check that the API integrations are working on your site.

WordPress Plugin: Foxpost, MPL, and GLS Integration

1. Set Up Your WordPress Plugin

  1. Create Directory Structure:

    • Navigate to wp-content/plugins in your WordPress installation.
    • Create a folder named wp-foxpost.
  2. Add Files:

    • wp-foxpost.php (in wp-foxpost/):

      <?php
      /*
       Plugin Name: Foxpost Integration
       Description: Integrates Foxpost, MPL, and GLS APIs.
       Version: 1.0
       Author: Your Name
      */
      
      if ( ! defined( 'ABSPATH' ) ) {
          exit; // Exit if accessed directly.
      }
      
      include plugin_dir_path( __FILE__ ) . 'includes/class-api-service.php';
      include plugin_dir_path( __FILE__ ) . 'includes/admin-settings.php';
      
      function wp_foxpost_menu() {
        add_options_page( 'Foxpost Settings', 'Foxpost', 'manage_options', 'foxpost-settings', 'wp_foxpost_settings_page' );
      }
      add_action( 'admin_menu', 'wp_foxpost_menu' );
      
      function wp_foxpost_settings_page() {
        ?>
        <div class="wrap">
          <h1>Foxpost Settings</h1>
          <form method="post" action="options.php">
            <?php
              settings_fields( 'foxpost_options_group' );
              do_settings_sections( 'foxpost-settings' );
              submit_button();
            ?>
          </form>
        </div>
        <?php
      }
      
    • includes/class-api-service.php (in wp-foxpost/includes/):

      <?php
      
      class ApiService {
        public function fetchData($endpoint, $params) {
          $response = wp_remote_get( add_query_arg( $params, $endpoint ) );
          return json_decode( wp_remote_retrieve_body( $response ), true );
        }
      }
      
    • includes/admin-settings.php (in wp-foxpost/includes/):

      <?php
      
      function wp_foxpost_settings() {
        register_setting( 'foxpost_options_group', 'foxpost_api_key' );
        register_setting( 'foxpost_options_group', 'mpl_api_key' );
        register_setting( 'foxpost_options_group', 'gls_api_key' );
      
        add_settings_section( 'foxpost_settings_section', 'API Settings', null, 'foxpost-settings' );
      
        add_settings_field( 'foxpost_api_key', 'Foxpost API Key', 'wp_foxpost_api_key_callback', 'foxpost-settings', 'foxpost_settings_section' );
        add_settings_field( 'mpl_api_key', 'MPL API Key', 'wp_mpl_api_key_callback', 'foxpost-settings', 'foxpost_settings_section' );
        add_settings_field( 'gls_api_key', 'GLS API Key', 'wp_gls_api_key_callback', 'foxpost-settings', 'foxpost_settings_section' );
      }
      
      function wp_foxpost_api_key_callback() {
        $value = get_option( 'foxpost_api_key', '' );
        echo '<input type="text" id="foxpost_api_key" name="foxpost_api_key" value="' . esc_attr( $value ) . '" />';
      }
      
      function wp_mpl_api_key_callback() {
        $value = get_option( 'mpl_api_key', '' );
        echo '<input type="text" id="mpl_api_key" name="mpl_api_key" value="' . esc_attr( $value ) . '" />';
      }
      
      function wp_gls_api_key_callback() {
        $value = get_option( 'gls_api_key', '' );
        echo '<input type="text" id="gls_api_key" name="gls_api_key" value="' . esc_attr( $value ) . '" />';
      }
      
      add_action( 'admin_init', 'wp_foxpost_settings' );
      

2. Upload and Activate the Plugin

  1. Package:

    • Zip the wp-foxpost directory.
  2. Upload:

    • Go to Admin > Plugins > Add New > Upload Plugin.
    • Upload the .zip file and click Install Now.
  3. Activate:

    • After installation, click Activate Plugin.
  4. Configure:

    • Go to Admin > Settings > Foxpost Settings.
    • Enter API credentials for Foxpost, MPL, and GLS.
  5. Test:

    • Verify that the API integrations work correctly on your site.

This guide simplifies the process of integrating Foxpost,

MPL, and GLS APIs into Drupal and WordPress. Follow these steps to set up your modules and plugins, ensuring smooth API integration.