<?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: Raymundo CH</title>
    <description>The latest articles on DEV Community by Raymundo CH (@raymundoch).</description>
    <link>https://dev.to/raymundoch</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%2F3163350%2Fc871ccc4-d0a6-4dbd-b0cb-7c98fff0cda6.jpg</url>
      <title>DEV Community: Raymundo CH</title>
      <link>https://dev.to/raymundoch</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raymundoch"/>
    <language>en</language>
    <item>
      <title>Git: using --interactive mode to commit specific parts of a file</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Tue, 17 Jun 2025 02:02:06 +0000</pubDate>
      <link>https://dev.to/raymundoch/git-using-interactive-mode-to-commit-specific-parts-of-a-file-pj1</link>
      <guid>https://dev.to/raymundoch/git-using-interactive-mode-to-commit-specific-parts-of-a-file-pj1</guid>
      <description>&lt;p&gt;Sometimes you want to commit only a few sections of a file rather than the whole file. In order to have more control over the contents of a commit we can implement the &lt;strong&gt;--interactive&lt;/strong&gt; mode of the &lt;strong&gt;git add&lt;/strong&gt; command.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tracking the files with Git
&lt;/h2&gt;

&lt;p&gt;Let's say that you just created a file called &lt;strong&gt;fruits.txt&lt;/strong&gt; within the working directory and you want to save its contents into two different commits. &lt;/p&gt;

&lt;p&gt;The file contains two words:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bananas
Apples
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By running the command &lt;strong&gt;git status&lt;/strong&gt; we can see that the file is untracked:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
        modified:   countries.txt
        modified:   spanish.txt

Untracked files:
  (use "git add &amp;lt;file&amp;gt;..." to include in what will be committed)
        fruits.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly make Git track the file; in other words make Git aware that the file exists by running the next command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add -N fruits.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run &lt;strong&gt;git status&lt;/strong&gt; and check out the output; now the file &lt;strong&gt;fruits.txt&lt;/strong&gt; appears under the &lt;strong&gt;Changes not staged for commit&lt;/strong&gt;  heading which means Git it aware the file exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
        modified:   countries.txt
        new file:   fruits.txt
        modified:   spanish.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://siteground.com/woocommerce-hosting.htm?afimagecode=2724bc01d9f2de33f335aa001b9b8b59" rel="noopener noreferrer"&gt;&lt;img alt="Ad - WooCommerce hosting from SiteGround - The best home for your online store. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_woocommerce-leaderboard-violet.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the --interactive mode to customize the commit
&lt;/h2&gt;

&lt;p&gt;We can customize the contents of a commit not just on a file basis but on a line-by-line basis. &lt;/p&gt;

&lt;p&gt;The command &lt;strong&gt;git add&lt;/strong&gt;can be used to customize the contents inside the staging area; in other words we can customize the commit:&lt;/p&gt;

&lt;p&gt;Run the next command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add --interactive
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the prompt enter the keyword patch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What now&amp;gt; patch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my case the output looks as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   staged     unstaged path
  1:    unchanged        +1/-0 countries.txt
  2:        +0/-0        +2/-0 fruits.txt
  3:    unchanged        +1/-0 spanish.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the prompt enter the number corresponding to the file &lt;strong&gt;fruits.txt&lt;/strong&gt; as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Patch update&amp;gt;&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output looks as shown below in my case; note the asterisc next to the entry for the &lt;strong&gt;fruits.txt&lt;/strong&gt; file; which means such a file was selected. Press enter in the prompt as a confirmation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  staged     unstaged path
  1:    unchanged        +1/-0 countries.txt
* 2:        +0/-0        +2/-0 fruits.txt
  3:    unchanged        +1/-0 spanish.txt
Patch update&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the output displays the contents of the file; in this case two lines of text. Enter &lt;strong&gt;e&lt;/strong&gt; in the command prompt to indicate that we want to customize which line of text will be included in the next commit and which one won't be included:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index 0000000..9584ef5
--- /dev/null
+++ b/fruits.txt
@@ -0,0 +1,2 @@
+Bananas
+Apples
(1/1) Stage addition [y,n,q,a,d,e,?]? e

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

&lt;/div&gt;



&lt;p&gt;The text editor opens and in order to indicate that one line won't ne included into the next commit let's add a &lt;strong&gt;#&lt;/strong&gt; symbol at the beginning of the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Manual hunk edit mode -- see bottom for a quick guide.
@@ -0,0 +1,2 @@
+Bananas
#Apples
# ---
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the changes and exit the text editor. Now in the command prompt enter &lt;strong&gt;diff&lt;/strong&gt; to see what's included in the staging area :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What now&amp;gt; diff
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output contains the files within the staging area and an overview of the additions and deletions made to the file. In the command prompt enter the number which represents the file to see its contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          staged     unstaged path
  1:        +1/-0        +1/-0 fruits.txt
Review diff&amp;gt;&amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output indicates that the words &lt;strong&gt;Bananas&lt;/strong&gt; will be included into the next commit; note how the word  &lt;strong&gt;Apples&lt;/strong&gt; does not appear in the file anymore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index 0000000..2db3da3
--- /dev/null
+++ b/fruits.txt
@@ -0,0 +1 @@
+Bananas

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

&lt;/div&gt;



&lt;p&gt;In the command promt enter &lt;strong&gt;quit&lt;/strong&gt; to exit the &lt;strong&gt;--interactive mode&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;What now&amp;gt; quit
Bye.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in order to see the contents of the file &lt;strong&gt;fruits.txt&lt;/strong&gt; which ain't be included into the commit run the next command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git diff fruits.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the output looks as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;diff --git a/fruits.txt b/fruits.txt
index 2db3da3..9584ef5 100644
--- a/fruits.txt
+++ b/fruits.txt
@@ -1 +1,2 @@
 Bananas
+Apples
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The word &lt;strong&gt;Apples&lt;/strong&gt; is not in the staging area and won't be included into the next commit. &lt;/p&gt;

&lt;h2&gt;
  
  
  Commit the changes
&lt;/h2&gt;

&lt;p&gt;Run the next command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m "some comment on the commit"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the new commit will be created. Run &lt;strong&gt;git log&lt;/strong&gt; to see the listing of commits available:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;commit a722eda865765382bc3e88a0e750d6f316f22c0a (HEAD -&amp;gt; main)
Author: Raymunco CH &amp;lt;no@email.com&amp;gt;
Date:   Mon Jun 16 19:50:14 2025 -0600

    commit for Bananas

more commits appear here in my case....
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Commiting the remaining text line
&lt;/h2&gt;

&lt;p&gt;The word &lt;strong&gt;Apples&lt;/strong&gt; remains unstaged and uncommited therefore let's create a commit for it. &lt;/p&gt;

&lt;p&gt;Run the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add fruits.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the file is into the staging area; let's run the next command to see the contents added to the staging area:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git diff --staged
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the output looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;--- a/fruits.txt
+++ b/fruits.txt
@@ -1 +1,2 @@
 Bananas
+Apples
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the word &lt;strong&gt;Apples&lt;/strong&gt; is staged and ready to be commited then run the next command_&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m "Commit for apples"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it!! the contents of the file &lt;strong&gt;fruits.txt&lt;/strong&gt; were splitted into two commits.&lt;/p&gt;

&lt;p&gt;Happy Coding!!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/woocommerce-hosting.htm?afimagecode=2724bc01d9f2de33f335aa001b9b8b59" rel="noopener noreferrer"&gt;&lt;img alt="Ad - WooCommerce hosting from SiteGround - The best home for your online store. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_woocommerce-leaderboard-violet.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>Git : How to commit a few lines of code from several files?</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Sun, 15 Jun 2025 01:54:16 +0000</pubDate>
      <link>https://dev.to/raymundoch/git-how-to-commit-a-few-lines-of-code-from-a-file-22eh</link>
      <guid>https://dev.to/raymundoch/git-how-to-commit-a-few-lines-of-code-from-a-file-22eh</guid>
      <description>&lt;p&gt;Most of the times when making a commit with Git the files are staged and then those files are commited. Nevertheless sometimes we need to split a file into chunks and save those chunks into different commits. In other words sometimes we might need to commit a few lines of code only rather than the whole file. Here I share with you how to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding the file to te staging area
&lt;/h2&gt;

&lt;p&gt;First we must add the file to the staging area in case the file is untracked by Git. In this case I want to add the file inside the Deactivation folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;On branch main
Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
        modified:   tinyshortcode.php

Untracked files:
  (use "git add &amp;lt;file&amp;gt;..." to include in what will be committed)
        Deactivation/
        FilterCurrency/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I have to run the next Git command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add -N Deactivation/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the staging area looks as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;On branch main
Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
        new file:   Deactivation/register.php
        modified:   tinyshortcode.php

Untracked files:
  (use "git add &amp;lt;file&amp;gt;..." to include in what will be committed)
        FilterCurrency/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Picking up the desired lines in the first file
&lt;/h2&gt;

&lt;p&gt;The commit will comprise lines of code from the files &lt;strong&gt;register.php&lt;/strong&gt; and &lt;strong&gt;tinyshortcode.php&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's work with the file &lt;strong&gt;register.php&lt;/strong&gt; by running the next Git command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git add --patch Deactivation/register.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And let's enter the option &lt;strong&gt;e&lt;/strong&gt; into the command prompt; &lt;strong&gt;e&lt;/strong&gt; stands for &lt;strong&gt;edit&lt;/strong&gt; and enables you to choose the lines of code you want to save in the next commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(1/1) Stage addition [y,n,q,a,d,e,?]? e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A text editor is opened in the console where you have to use &lt;strong&gt;#&lt;/strong&gt; at the beginning  of the lines which you want to remove from the commit; the remaining lines will be included into the commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@@ -0,0 +1,12 @@
+&amp;lt;?php
+
+function rch_register_deactivation_logic(){
+
#    delete_option("fruitsTableData");
+}
+
+
+register_deactivation_hook(
+    WP_PLUGIN_DIR."/tinyShortcode/tinyshortcode.php",
+    "rch_register_deactivation_logic"
+);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take a closer look at the code above and see how only one line of the code will be removed and the remaining lines will be saved into the commit; the lines of code which start with &lt;strong&gt;+&lt;/strong&gt; will be commited.&lt;/p&gt;

&lt;p&gt;Run the next command to see the changes made. In other words you will see the lines of code to be included into the next commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git diff --cached
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also check out the staging area and see how it looks different:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;On branch main
Changes to be committed:
  (use "git restore --staged &amp;lt;file&amp;gt;..." to unstage)
        new file:   Deactivation/register.php

Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
        modified:   Deactivation/register.php
        modified:   tinyshortcode.php

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Picking up the desired lines of the second file
&lt;/h2&gt;

&lt;p&gt;Since the second file is already located in the staging area then we just have to run the Git command necessary to choose a few lines of code for the next commit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; git add --patch tinyshortcode.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A promt is displayed where we have to enter the option &lt;strong&gt;e&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(1/1) Stage this hunk [y,n,q,a,d,e,?]? e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A text editor is opened where  we have to choose the lines to be removed from the commit by adding a &lt;strong&gt;#&lt;/strong&gt; at the beginning of the line:&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 WP_PLUGIN_DIR."/tinyShortcode/Deactivation/register.php";
#require_once WP_PLUGIN_DIR."/tinyShortcode/Activation/register.php";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the staging area looks as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;On branch main
Changes to be committed:
  (use "git restore --staged &amp;lt;file&amp;gt;..." to unstage)
        new file:   Deactivation/register.php
        modified:   tinyshortcode.php

Changes not staged for commit:
  (use "git add &amp;lt;file&amp;gt;..." to update what will be committed)
  (use "git restore &amp;lt;file&amp;gt;..." to discard changes in working directory)
        modified:   Deactivation/register.php
        modified:   tinyshortcode.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember that you have to run the next command to see the staging area:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's commit the changes
&lt;/h2&gt;

&lt;p&gt;Once the staging area contains the lines of code we want to include in the next commit it's time to save the changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m "Add a description here..."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to see the lines of code of the newly created commit with the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git show [commit hash]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in order to see the listing of commits with their corresponding hashes use the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading this article!! see you soon! Happy coding &lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Wordpress development - shortcodes and arrays as parameters</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Thu, 05 Jun 2025 02:03:11 +0000</pubDate>
      <link>https://dev.to/raymundoch/wordpress-development-shortcodes-and-arrays-as-parameters-1pm5</link>
      <guid>https://dev.to/raymundoch/wordpress-development-shortcodes-and-arrays-as-parameters-1pm5</guid>
      <description>&lt;p&gt;When inserting a shortcode into the Wordpress editor we can use strings or numbers as parameters; but what if we want to pass in an array as a parameter? How can we code a shortcode which handles arrays as parameters? &lt;/p&gt;

&lt;p&gt;In this article I'll cover how to code a shortcode which receives arrays as parameters as well as how to pass in an array to a shortcode in the Wordpress editor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calling the add_shortcode function
&lt;/h2&gt;

&lt;p&gt;First the add_shortcode must be called to configure a new shortcode; this function requires two parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function rch_handle_render_table( $atts = [] ){}

 //shortcode logic will be here...

}
add_shortcode("render_table","rch_handle_render_table");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how the name of the function is prefix; this is important to prevent collisions with other functions from other plugins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting the array of parameters
&lt;/h2&gt;

&lt;p&gt;Sometimes users insert a shortcode and when adding the parameters the user mistypes the parameters; let's say the name of the parameter is &lt;strong&gt;paramone&lt;/strong&gt; and the user types in &lt;strong&gt;ParamOne&lt;/strong&gt; which contains uppercases. That's the reason why the names of the parameters must be turned into lowercase so that the php code does not crash.&lt;/p&gt;

&lt;p&gt;See the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    $formattedAtts = array_change_key_case(
          $atts,
          CASE_LOWER
    );

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

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;$atts&lt;/strong&gt; array holds the parameters and the names of the parameters are the keys.&lt;/p&gt;

&lt;h2&gt;
  
  
  Unserializing the arrays passed in as parameters
&lt;/h2&gt;

&lt;p&gt;Passing in the array directly into the shortcode when using the Wordpress editor is not possible; you must transform the array into a text string with an special format; that process is called serialization.&lt;/p&gt;

&lt;p&gt;When the parameters are received they must be unserialized so that they can be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  if(isset($formattedAtts['header'])){

        $formattedAtts['header'] = unserialize($formattedAtts['header']);

    }

     if(isset($formattedAtts['body'])){

        $formattedAtts['body'] = unserialize($formattedAtts['body']);

    }

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Overwriting the default parameters
&lt;/h2&gt;

&lt;p&gt;Shortcodes must be able to work is case the parameters ain't passed in; therefore default parameters must be declared and overwritten only if the user passes in the required parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $finalAtts = shortcode_atts(
        array(
            "header" =&amp;gt; array("#","Fruit Name","Fruit Price"),
            "body" =&amp;gt; array(
                array(
                    "Fruit name 1","Fruit price 1"
                ),
                array(
                    "Fruit name 2","Fruit price 2"
                )
            )
        ),
        $formattedAtts
    );

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

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;shortcode_atts&lt;/strong&gt; function call replaces the default parameters with the corresponding parameters passed in. The first array represents the default parameters.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Generating the header of the table
&lt;/h2&gt;

&lt;p&gt;In this case the shortcode displays a table; the php code necessary to generate the table header looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $htmlMarkup = "&amp;lt;table&amp;gt;
           &amp;lt;thead&amp;gt;&amp;lt;tr&amp;gt;";

    foreach($finalAtts['header'] as $value ){

        $htmlMarkup .=  "&amp;lt;th&amp;gt;".$value."&amp;lt;/th&amp;gt;";
    }

    $htmlMarkup .= "&amp;lt;/tr&amp;gt;&amp;lt;/thead&amp;gt;";


    $htmlMarkup .= "&amp;lt;tbody&amp;gt;";

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generating the table body
&lt;/h2&gt;

&lt;p&gt;The php code necessary to render the body of the table looks as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; foreach($finalAtts['body'] as $index =&amp;gt; $singlerow ){

        $rowNumber = $index + 1;

        $htmlMarkup .= "&amp;lt;tr&amp;gt;&amp;lt;td&amp;gt;".$rowNumber."&amp;lt;/td&amp;gt;";

        foreach($singlerow as $value ){

            $htmlMarkup .= "&amp;lt;td&amp;gt;".$value."&amp;lt;/td&amp;gt;";

        }

        $htmlMarkup .= "&amp;lt;/tr&amp;gt;";
    }

    $htmlMarkup .= "&amp;lt;/tbody&amp;gt;
    &amp;lt;/table&amp;gt;";

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inserting the shortcode
&lt;/h2&gt;

&lt;p&gt;Open the Wordpress block editor and insert the shortcode by using the shortcode block.&lt;/p&gt;

&lt;p&gt;Below appears an screenshot of the shortcode with both parameters being used. Note how the values for the parameters are serialized text strings:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjgaer9fg18q6uo9dmn83.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjgaer9fg18q6uo9dmn83.png" alt="Image description" width="783" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember that the values for the parameters must be arrays and also don't forget that there are many free online tools where you can serialize your arrays in a few clicks.&lt;/p&gt;

&lt;p&gt;Below appears an screenshot of the &lt;strong&gt;render_table&lt;/strong&gt; shortcode in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76urwengdkozgy4wpdhx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F76urwengdkozgy4wpdhx.png" alt="Image description" width="404" height="213"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>wordpress</category>
      <category>programming</category>
      <category>php</category>
    </item>
    <item>
      <title>Wordpress development - creating a shortcode with parameters</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Tue, 03 Jun 2025 03:16:30 +0000</pubDate>
      <link>https://dev.to/raymundoch/wordpress-development-creating-a-shortcode-41p7</link>
      <guid>https://dev.to/raymundoch/wordpress-development-creating-a-shortcode-41p7</guid>
      <description>&lt;p&gt;Shortcodes are the method provided by Wordpress to embeed php code snippets into either a post or page. Since you are able to embeed custom php code into a publication therefore the possibilities are endless!&lt;/p&gt;

&lt;p&gt;In this tutorial we are going to code a shortcode which renders a Table to the web browser; it can handle parameters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calling the add_shortcode function
&lt;/h2&gt;

&lt;p&gt;Call the add_shortcode function and declare the callback function which runs when this shortcode is embeeded into a publication:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handle_prices_table_shortcode($atts = [], $content = null){

//here the shortcode logic will be placed...

}

add_shortcode("prices_table","handle_prices_table_shortcode");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above shortcode can be embeeded into a publication by inserting the text string &lt;strong&gt;[prices_table]&lt;/strong&gt; into a publication's content.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking the shortcode type
&lt;/h2&gt;

&lt;p&gt;Inside the function declaration add the next lines of php code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if($content){
    return;
 }

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

&lt;/div&gt;



&lt;p&gt;In case the shortcode is used to enclose content then nothing is printed by the shortcode because the shortcode is not design to work with enclosed content. Shortcodes which enclose content are other type of shortcode and they won't be covered here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting parameters
&lt;/h2&gt;

&lt;p&gt;When the users add parameters to the shortcode they can mistype the parameter and use uppercase letters like &lt;strong&gt;ParamOne&lt;/strong&gt; instead of &lt;strong&gt;paramone&lt;/strong&gt;; turning all parameters names into lowercase prevents the code from crashing.&lt;/p&gt;

&lt;p&gt;Add the next lines of php code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$formattedAtts = array_change_key_case(
      $atts,
      CASE_LOWER
  );

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

&lt;/div&gt;



&lt;p&gt;The previous php code transforms the parameters names into lowercase; the names of the parameters are saved as keys into an array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Overriding default parameters
&lt;/h2&gt;

&lt;p&gt;Chances are the user forgets to insert parameters. In that case the default parameters will be used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$finalAtts = shortcode_atts(
    array(
        "header" =&amp;gt; array("#","Fruit Name","Fruit Price"),
        "body" =&amp;gt; array(
            array("Fruit 1","Price 1"),
            array("Fruit 2","Price 2")
        )
    ),
    $formattedAtts
);

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

&lt;/div&gt;



&lt;p&gt;The first array contains the default values; these values will be overriden with the values provided by the &lt;strong&gt;$formattedAtts&lt;/strong&gt; array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the header of the table
&lt;/h2&gt;

&lt;p&gt;The first parameter will contain the table header text strings; these values will be transformed into HTML markup as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$htmlMarkup = "&amp;lt;table class='table'&amp;gt;
       &amp;lt;thead&amp;gt;&amp;lt;tr&amp;gt;";

       for($i = 0; $i &amp;lt;= count($finalAtts['header']); $i++ ){


          $htmlMarkup .= "&amp;lt;th&amp;gt;".$finalAtts['header'][$i]."&amp;lt;/th&amp;gt;";


       }

    $htmlMarkup .= "&amp;lt;/tr&amp;gt;&amp;lt;/thead&amp;gt;&amp;lt;tbody&amp;gt;";

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

&lt;/div&gt;



&lt;p&gt;In short the previous code snippet is iterating over the values of the &lt;strong&gt;$finalAtts['header']&lt;/strong&gt; array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the body of the table
&lt;/h2&gt;

&lt;p&gt;The next code snippet iterates over the values of the &lt;strong&gt;$finalAtts['body']&lt;/strong&gt; array and generates a row on each iteration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; foreach($finalAtts['body'] as $index =&amp;gt; $rowValues ){

        $htmlMarkup .= "&amp;lt;tr&amp;gt;";

        $num = $index + 1;

        $htmlMarkup .= "&amp;lt;td&amp;gt;".$num."&amp;lt;/td&amp;gt;";

          foreach($rowValues as $record ){

            $htmlMarkup .= "&amp;lt;td&amp;gt;".$record."&amp;lt;/td&amp;gt;";

          }

        $htmlMarkup .= "&amp;lt;/tr&amp;gt;";

    }


    $htmlMarkup .= "&amp;lt;/tbody&amp;gt;
  &amp;lt;/table&amp;gt;";

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Returning the HTML markup
&lt;/h2&gt;

&lt;p&gt;At the bottom a return statement must be placed; remember that shortcodes must return some value always:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  return $htmlMarkup;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following screenshot displays the table in action. In my case the Bootstrap CSS framework was added to the project so the look of your table will be a bit different:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabouxro2hl2du1uruilm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fabouxro2hl2du1uruilm.png" alt="Image description" width="577" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>wordpress</category>
      <category>php</category>
      <category>programming</category>
    </item>
    <item>
      <title>Wordpress REST API - URL parameters</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Mon, 02 Jun 2025 20:58:00 +0000</pubDate>
      <link>https://dev.to/raymundoch/wordpress-rest-api-url-parameters-3nm</link>
      <guid>https://dev.to/raymundoch/wordpress-rest-api-url-parameters-3nm</guid>
      <description>&lt;p&gt;When working with the Wordpress REST API we can create our own endpoints and configure them according to our needs; one important feature of any API are URL parameters because they enable HTTP requests to include additional information. Let's analyze how to add those parameters to a given API endpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rest_api_init action hook
&lt;/h2&gt;

&lt;p&gt;Each time we want to add custom endpoint we have to implement the rest_api_init action hook as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
function rch_handle_params_request(){

 //call to register_rest_route() will be here...

}


add_action("rest_api_init","rch_handle_params_request");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The register_rest_route function
&lt;/h2&gt;

&lt;p&gt;This function is required to configure an endpoint from scratch and as you can see it uses several parameters:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; register_rest_route(
        "customAPI/v1",
        "/post/(?P&amp;lt;id&amp;gt;\d+)",
        array(
            "methods" =&amp;gt; "GET",
            "callback" =&amp;gt; "rch_get_custom_post"
        )
    );

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

&lt;/div&gt;



&lt;p&gt;The parameters are explained next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;customAPI/v1 is the namespace or a way to group endpoints; it's like a prefix and must be unique throughout the API.&lt;/li&gt;
&lt;li&gt;/post/(?P\d+) is the URL parameter configuration; note how a regular expression is used to check the format of the id number.&lt;/li&gt;
&lt;li&gt;The HTTP method is set to GET which enables the route to have support for GET requests only.&lt;/li&gt;
&lt;li&gt;The callback parameter is set to a reference to the function which handles the HTTP request and generates the reponse.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Breaking down the callback function
&lt;/h2&gt;

&lt;p&gt;The next code snippet must be placed outside the &lt;strong&gt;rch_handle_params_request&lt;/strong&gt; function declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function rch_get_custom_post($request){


    $posts = get_posts(array(
        "numberposts" =&amp;gt; -1,
        "post_type" =&amp;gt; "post",
        "post_status" =&amp;gt; "publish",
        "p" =&amp;gt; $request['id']
    ));

    if(!$posts){

        return rest_ensure_response(new WP_Error(
            "rest_not_found",
            "The post does not exist",
            array(
                "status" =&amp;gt; 404
            )
        ));

    }else{

        return rest_ensure_response($posts);

    }
}


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

&lt;/div&gt;



&lt;p&gt;I'm gonna try to sum up all the previous code so that you can understand it faster.&lt;/p&gt;

&lt;p&gt;The get_posts function retrieves publications from Wordpress; two parameters are not very obvious; the first one is numberposts =&amp;gt; -1 which indicates that all the posts matching the criteria must be retrieved and  "p" =&amp;gt; $request['id'] indicates that the id number of the matching publication must be equal to the property id of the request object; this property contains the value passed in via the URL parameter. &lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;if-block&lt;/strong&gt; checks whether the &lt;strong&gt;$posts&lt;/strong&gt; array is empty or not; when the &lt;strong&gt;$posts&lt;/strong&gt; array is empty then  an error object is retured otherwise the matching publication is returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Wordpress REST API - creating your first custom endpoint</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Sun, 25 May 2025 03:22:29 +0000</pubDate>
      <link>https://dev.to/raymundoch/wordpress-rest-api-creating-your-first-custom-endpoint-572h</link>
      <guid>https://dev.to/raymundoch/wordpress-rest-api-creating-your-first-custom-endpoint-572h</guid>
      <description>&lt;p&gt;Wordpress provides access to a REST API which can be implemented to transform a Wordpress installation into a Headless CMS. Therefore the frontend can be created by using any framework like React or Angular while the data is stored into Wordpress and consumed by means of the built-in REST API.&lt;/p&gt;

&lt;p&gt;You can also configure custom endpoints; in order words you can create your own API and include it into a Wordpress installation.&lt;/p&gt;

&lt;p&gt;This short tutorial covers how to add a custom endpoint to the Wordpress REST API.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a new plugin and activating it
&lt;/h2&gt;

&lt;p&gt;In order to create a custom API endpoint a new plugin must be created and activated so head over to the Plugins folder and add the folder called restPlugin inside; then create the file **restplugin.php **and add the next php code snippet:&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
/* 
Plugin Name: Rest Plugin
Description: This plugins generates a testing API
Author: YOUR-NAME
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The previous comment provides metadata regarding the plugin and it's used to indicate Wordpress that this folder is a plugin; also the metadata is shown within the &lt;strong&gt;Plugins screen&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then head over to the &lt;strong&gt;Dashboard&lt;/strong&gt; and inside the &lt;strong&gt;Plugins screen&lt;/strong&gt; look for the plugin and activate it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding a new file to the project
&lt;/h2&gt;

&lt;p&gt;Add a new php file called &lt;strong&gt;register.php&lt;/strong&gt;; this is the file where the php code will be added later on but before that it must be included into the project so open the file &lt;strong&gt;restplugin.php&lt;/strong&gt; and add the next line below the :&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 WP_PLUGIN_DIR."/restPlugin/register.php";

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the API endpoint
&lt;/h2&gt;

&lt;p&gt;Open the register.php file and add the next code snippet inside:&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 rch_handle_welcome_message(){

    return rest_ensure_response("Welcome to the restplugin API");
}


function rch_register_api_routes(){


    register_rest_route(
        "restplugin/v1",
        "/welcome-message",
       array(
        "methods" =&amp;gt; "GET",
        "callback" =&amp;gt; "rch_handle_welcome_message"
       )
    );
}


add_action("rest_api_init","rch_register_api_routes");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;rest_api_init&lt;/strong&gt; hook is used to configure the REST API. The &lt;strong&gt;register_rest_route()&lt;/strong&gt; function is called to add the new endpoint to the API. &lt;/p&gt;

&lt;p&gt;The first parameter passed in to the function is called the &lt;strong&gt;namespace&lt;/strong&gt; and it's like a prefix used to classify the endpoints or group the endpoints into a category. The &lt;strong&gt;namespace&lt;/strong&gt; is very important because it prevents the endpoints from crashing with other endpoints from another plugin.&lt;/p&gt;

&lt;p&gt;The second parameter is the &lt;strong&gt;path&lt;/strong&gt; to the API resource; paths must also be unique within the same &lt;strong&gt;namespace&lt;/strong&gt; to avoid collisions with other paths for other API resources.&lt;/p&gt;

&lt;p&gt;The combination of the &lt;strong&gt;namespace&lt;/strong&gt; and the &lt;strong&gt;path&lt;/strong&gt; forms the route; later on it will be shown how to implement the route to access a given API resource.&lt;/p&gt;

&lt;p&gt;The third parameter is an array which describes the &lt;strong&gt;HTTP methods&lt;/strong&gt; allowed for the newly added API endpoint; in this case the &lt;strong&gt;GET method&lt;/strong&gt; only. The function which handles the &lt;strong&gt;HTTP request&lt;/strong&gt; for a given &lt;strong&gt;route&lt;/strong&gt; and &lt;strong&gt;HTTP method&lt;/strong&gt; is known as the &lt;strong&gt;endpoint&lt;/strong&gt; and is indicated into the array as well.&lt;/p&gt;

&lt;p&gt;Note that some people define an endpoint as the specific address for a given API resource which includes the &lt;strong&gt;HTTP method&lt;/strong&gt; and the &lt;strong&gt;route&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/woocommerce-hosting.htm?afimagecode=2724bc01d9f2de33f335aa001b9b8b59" rel="noopener noreferrer"&gt;&lt;img alt="Ad - WooCommerce hosting from SiteGround - The best home for your online store. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_woocommerce-leaderboard-violet.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Making an HTTP request to the API
&lt;/h2&gt;

&lt;p&gt;Open the web browser and enter the next address into the address bar:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="http://localhost/index.php/wp-json/restplugin/v1/welcome-message" rel="noopener noreferrer"&gt;http://localhost/index.php/wp-json/restplugin/v1/welcome-message&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In case the previous adress does not work you could also try the following address without the &lt;strong&gt;/index.php&lt;/strong&gt; part:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="http://localhost/wp-json/restplugin/v1/welcome-message" rel="noopener noreferrer"&gt;http://localhost/wp-json/restplugin/v1/welcome-message&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should see the text "Welcome to the restplugin API" on the screen! &lt;/p&gt;

&lt;p&gt;Thanks for reading through this tutorial! see you soon!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/woocommerce-hosting.htm?afimagecode=2724bc01d9f2de33f335aa001b9b8b59" rel="noopener noreferrer"&gt;&lt;img alt="Ad - WooCommerce hosting from SiteGround - The best home for your online store. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_woocommerce-leaderboard-violet.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>wordpress</category>
      <category>php</category>
      <category>programming</category>
    </item>
    <item>
      <title>Wordpress development - configure a theme template by using a plugin</title>
      <dc:creator>Raymundo CH</dc:creator>
      <pubDate>Wed, 14 May 2025 22:28:01 +0000</pubDate>
      <link>https://dev.to/raymundoch/wordpress-development-configure-a-theme-template-by-using-a-plugin-5262</link>
      <guid>https://dev.to/raymundoch/wordpress-development-configure-a-theme-template-by-using-a-plugin-5262</guid>
      <description>&lt;p&gt;When you are developing a plugin you might come across a situatuion where you have to update a theme template or create a new one from scratch.&lt;/p&gt;

&lt;p&gt;Under the previous circumstances the solution I've implemented is the use of the &lt;strong&gt;template_include filter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I'm going to explain how this filter works and it's quite interesting and useful.&lt;/p&gt;

&lt;p&gt;First call the &lt;strong&gt;add_filter()&lt;/strong&gt; function as shown below and pass in the filter name as parameter:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&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 rch_add_custom_post_type_template($template){


    return $template;


}

add_filter("template_include","rch_add_custom_post_type_template");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how the function receives the &lt;strong&gt;$template&lt;/strong&gt; variable which holds the path to the php file which contains the HTML markup and the php code.&lt;/p&gt;

&lt;p&gt;Then let's add the logic to filter the web pages and locate the one we want to modify:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function rch_add_custom_post_type_template($template){

    if(is_singular("mycars")){


    }

    return $template;


}

add_filter("template_include","rch_add_custom_post_type_template");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note how the &lt;strong&gt;is_singular()&lt;/strong&gt; function was implemented to find the publication which contains content related to the custom post type called &lt;strong&gt;mycars&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Then the path to the template must be saved into a variable and returned as the result of the function:&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_singular("mycars")){

    $template_path = WP_PLUGIN_DIR."/carsplugin/frontend/carsTemplate.php";

        if(file_exists($template_path)){

            return $template_path;
        }

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

&lt;/div&gt;



&lt;p&gt;Remember that the constant called &lt;strong&gt;WP_PLUGIN_DIR&lt;/strong&gt; holds the path to the plugins folder; it's important to note the difference between path and URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://siteground.com/wordpress-hosting.htm?afimagecode=a4bf640dbf051dfa419cb5b6d1226bf4" rel="noopener noreferrer"&gt;&lt;img alt="Ad - Managed WordPress Hosting from SiteGround - Powerful, yet simple to use. Click to learn more." src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsiteground.com%2Fstatic%2Faffiliate%2Fen%2FNOPRICE%2Fgeneral_EN_wordpress-leaderboard-blue.jpg" width="728" height="90"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>webdev</category>
      <category>php</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
