Programming

Converting multiple XML files to single JSON by PHP

 

Hits: 5

Standard
Programming, WordPress

PHP code to Import the taxonomy terms from one website to another

1. Create a folder in the root directory of the site i.e PARENT_SITE from where we want to export the Taxonomy Terms. I have created services folder in the root directory and created the php file named books-category.php

2. Include wp-load.php the files so that we could use all the WordPress available functions

3. Write a code that echo’ s JSON representation of a terms related to book taxonomy . We can replace the taxonomy name by any other taxonomy we want to import

 

We can check the encoded taxonomy terms in the Url : http://PARENT_SITE/services/books-category.php

4. Create a folder in the root directory of the site I call it as CHILD_SITE where we want to import the Taxonomy Terms.

I have created app folder in the root directory of CHILD_SITE and created the php file named import-books-taxonomy.php :

5. Create a two meta field to store the parent id and the id of taxonomy terms OF the PARENT_SITE in the same taxonomy of CHILD_SITE .

We can use acf to create the meta/custom field in the taxonomy : https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

6. We can Use Guzzle to parse the json response from the MAIN_SITE : http://docs.guzzlephp.org/en/stable/overview.html

7. Write a code to read the JSON formated terms and insert terms in database

8. When imported, term is inserted in the database of the CHILD_SITE, the id of the inserted term is different than the PARENT_SITE term id . Therefore, PARENT_SITE term_id and parent_id of terms are saved in the CHILD_SITE of related taxonomy terms, so that it could be usedful in the future.

9. Run File to import the taxonomy terms, http://CHILD_SITE/app/books-category.php

Hits: 6

Standard
Programming, WordPress

Display first 8 words from the Sentence

My client asked  me to display only 8 words of the long title of the post. So, to fulfill his requirement,  I just count the number of words in  the title and if the title consist of more than 8 words, I extract 8 words from the title and displayed it , otherwise displayed the title as it is.

Steps 1 :   Create function that returns the first 8 words from the title/sentence, place it anywhere in your function.php or helpers.php files

Step 2 : Count the number of words in the title and use the function in step 1, to extract 8 words form the title. If the title has less than 8 words, do nothing just display the title.

Hits: 2

Standard
Programming, WordPress

Translating the month name

For translating the date in a website, we can have different solutions. But here,  I am particularly discussing about find and replace method for date translation.

Lets say we have date in format F j, Y i.e May 7, 2019 where,  Month is in English Langauge but what if you want to dispaly month in spanish or any other languages i.e. maio 7, 2019 ?

Just take care of the following steps and you can achive the translation for Month :

1. Create an array of Months where key is the default month and value is the translation for month.

2. Create function which replaces the default month with the translated month and returns the translated date string

3. Call the created funtion in step 2 and achieve the translation

 

Hits: 6

Standard
Programming, WordPress

Submitting WordPress Posts from Frontend

There might be a situation where you may want to allow your visitor to submit contents or articles or news of their own. Most of you might be thinking that ‘damn i shall find a plugin for this’, well this is not as hard as you think. You dont’ need to search for plugin. Writing code is easier than finding plugin and customizing it.

Follow the following simple steps ;

    1.  Create a form with necessary fields i.e. title, content and featured image. Additional field could be : visitor first name, last name , email and category. Don’t forget to add nonce field because it protects the forms from certain types of misuse and malicious .

       
    2. Validate the form  fields

       
    3.  If the user exists, get the user id ; if the user doesn’t exist, insert the user in the database and get the id of the inserted user . Note : wp_insert_user() gives the user id of just inserted user

4. Upload the attached file inside the uploads folder and get the id of the attached file .

 

5.   Save the contents including featured image  in the dashboard .  Note : save the contents as a draft so that you can review the post and publish it later.

 

Time for writing the full code :

 

You might not believe, but you have just created the form to  allow visitors to add articles/ news/ posts . And the most important part of this form is , you can customize the form  without any hassle.

Hits: 18

Standard