<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: it's volkan 😎</title>
    <description>The latest articles on DEV Community by it's volkan 😎 (@mevolkan).</description>
    <link>https://dev.to/mevolkan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F24666%2Fd471e4cb-41a8-44b1-9791-f28727eed5ad.jpg</url>
      <title>DEV Community: it's volkan 😎</title>
      <link>https://dev.to/mevolkan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mevolkan"/>
    <language>en</language>
    <item>
      <title>Creating a WordPress plugin: the basics</title>
      <dc:creator>it's volkan 😎</dc:creator>
      <pubDate>Tue, 10 Oct 2023 08:14:48 +0000</pubDate>
      <link>https://dev.to/mevolkan/creating-a-custom-plugin-the-basics-45p6</link>
      <guid>https://dev.to/mevolkan/creating-a-custom-plugin-the-basics-45p6</guid>
      <description>&lt;p&gt;This is based on an interview question I recently received while looking for a &lt;a href="https://wordpress.org/"&gt;WordPress &lt;/a&gt;developer job.&lt;/p&gt;

&lt;p&gt;I am using the following tools, ensure you have them installed&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://localwp.com/"&gt;Local&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using Local WP, create a sample project and ensure &lt;a href="https://woocommerce.com/"&gt;Woocommerce &lt;/a&gt;is installed.&lt;/p&gt;

&lt;p&gt;Scaffold the plugin using &lt;a href="https://wp-cli.org/"&gt;wp cli&lt;/a&gt; and use the shortcut from within local.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cSeqMFQO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2c00eukdusxmj7124g4x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cSeqMFQO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2c00eukdusxmj7124g4x.png" alt="Image description" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wp scaffold plugin woo-admin-sample&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will create a woo-admin-sample plugin folder,we can see it's also ready to activate.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NhKpPyWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quyf37zijh0sfnzdtbl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NhKpPyWI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/quyf37zijh0sfnzdtbl5.png" alt="Image description" width="800" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open the folder using vs code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MH3ePQmj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0pxa7urhyakplrdni14.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MH3ePQmj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0pxa7urhyakplrdni14.png" alt="Image description" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Open woo-admin-sample.php and add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // Plugin activation hook
register_activation_hook(__FILE__, 'my_api_plugin_activate');

function my_api_plugin_activate() {
    // Code to run when the plugin is activated
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a directory and inside the directory a new file settings-page.php. in this file, write the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
  add_filter( 'woocommerce_settings_tabs_array', 'sample_my_acount_add_settings_tab', 50 );
  function sample_my_acount_add_settings_tab( $settings_tabs ) {
    $settings_tabs['sample_my_account_tab'] = __( 'My Account', 'sample-my-account' );
    return $settings_tabs;
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create another file api-functions.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
function send_data_to_api() {
    // Code to send data to API
}

function store_api_data_as_transient() {
    // Code to store data as transients
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add another file templates.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
function load_templates($user_id) {
    // Code to load templates and display data
}

function check_data_expiry($user_id) {
    // Code to check data expiry and update if necessary
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add on to woo-admin-sample.php.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;require_once(plugin_dir_path(__FILE__) . 'includes/settings-page.php');
require_once(plugin_dir_path(__FILE__) . 'includes/api-functions.php');
require_once(plugin_dir_path(__FILE__) . 'includes/templates.php');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that we load the 3 files we added as part of the plugin files&lt;br&gt;
Now we can test and see that there is a 'My Account' tab under Woocommerce settings&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cA3wZ88Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4nug96z98dsh236yvxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cA3wZ88Z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4nug96z98dsh236yvxf.png" alt="Image description" width="800" height="373"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now add the API key field for admin and Options for other users in settings-page.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; add_action( 'woocommerce_settings_tabs_sample_my_account_tab', 'sample_my_account_tab' );
  function sample_my_account_tab() {
    woocommerce_admin_fields( sample_my_account_get_settings() );
  }

  function sample_my_account_get_settings() {

    $settings = array(
        'section_title' =&amp;gt; array(
            'name'     =&amp;gt; __( 'My Account Settings', 'sample-my-account' ),
            'type'     =&amp;gt; 'title',
            'desc'     =&amp;gt; '',
            'id'       =&amp;gt; 'wc_sample_my_account_tab_section_title'
        ),
        'api_key' =&amp;gt; array(
            'name' =&amp;gt; __( 'Api Key', 'sample-my-account' ),
            'type' =&amp;gt; 'text',
            'desc' =&amp;gt; __( 'Add API key', 'sample-my-account' ),
            'id'   =&amp;gt; 'wc_sample_my_account_tab_api_key'
        ),
        'options' =&amp;gt; array(
            'name' =&amp;gt; __( 'Options', 'sample-my-account' ),
            'type' =&amp;gt; 'textarea',
            'desc' =&amp;gt; __( 'Add options separated by a comma.', 'sample-my-account' ),
            'id'   =&amp;gt; 'wc_sample_my_account_tab_options'
        ),
        'section_end' =&amp;gt; array(
            'type' =&amp;gt; 'sectionend',
            'id' =&amp;gt; 'wc_sample_my_account_tab_section_end'
        )
    );

    return apply_filters( 'wc_sample_my_account_tab_settings', $settings );
}

add_action( 'woocommerce_update_options_sample_my_account_tab', 'sample_my_account_update_settings' );
  function cxc_update_settings() {
    woocommerce_update_options( sample_my_account_get_settings() );
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result will be 2 fields displayed, an Api key field and an Options field.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FjSq8Xsk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yymu6zg19z7slkz4j3j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FjSq8Xsk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yymu6zg19z7slkz4j3j.png" alt="Image description" width="800" height="444"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, the API key field is only visible for site owners, so let's amend our settings-page.php, replace the $settings array declaration with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    if (is_user_logged_in()) {
        $current_user_id = get_current_user_id();
        if (current_user_can('administrator')) {
            $settings = array(
                'section_title' =&amp;gt; array(
                    'name'     =&amp;gt; __('My Account Settings', 'sample-my-account'),
                    'type'     =&amp;gt; 'title',
                    'desc'     =&amp;gt; '',
                    'id'       =&amp;gt; 'wc_sample_my_account_tab_section_title'
                ),
                'api_key' =&amp;gt; array(
                    'name' =&amp;gt; __('Api Key', 'sample-my-account'),
                    'type' =&amp;gt; 'text',
                    'desc' =&amp;gt; __('Add API key', 'sample-my-account'),
                    'id'   =&amp;gt; 'wc_sample_my_account_tab_api_key'
                ),
                'options' =&amp;gt; array(
                    'name' =&amp;gt; __('Options', 'sample-my-account'),
                    'type' =&amp;gt; 'textarea',
                    'desc' =&amp;gt; __('Add options separated by a comma.', 'sample-my-account'),
                    'id'   =&amp;gt; 'wc_sample_my_account_tab_options'
                ),
                'section_end' =&amp;gt; array(
                    'type' =&amp;gt; 'sectionend',
                    'id' =&amp;gt; 'wc_sample_my_account_tab_section_end'
                )
            );
        } else {
            $settings = array(
                'section_title' =&amp;gt; array(
                    'name'     =&amp;gt; __('My Account Settings', 'sample-my-account'),
                    'type'     =&amp;gt; 'title',
                    'desc'     =&amp;gt; '',
                    'id'       =&amp;gt; 'wc_sample_my_account_tab_section_title'
                ),

                'options' =&amp;gt; array(
                    'name' =&amp;gt; __('Options', 'sample-my-account'),
                    'type' =&amp;gt; 'textarea',
                    'desc' =&amp;gt; __('Add options separated by a comma.', 'sample-my-account'),
                    'id'   =&amp;gt; 'wc_sample_my_account_tab_options'
                ),
                'section_end' =&amp;gt; array(
                    'type' =&amp;gt; 'sectionend',
                    'id' =&amp;gt; 'wc_sample_my_account_tab_section_end'
                )
            );
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can have the API key field load only for the admins and options for all users, we will refractor this later.&lt;/p&gt;

&lt;p&gt;PS: I am currently not employed and open to jobs.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
