Programming

Converting multiple XML files to single JSON by PHP

 

Hits: 2

Standard
ACF, Filter and Hooks, Programming, WordPress

Saving the CF7 submission in the Dashboard using ACF

1. First, create an acf options page. I am creating Cf7 Submissions as my option page where I will list all my form submissions values

2. Create a form using CF7. I am using the default form cf7 provides

3. Create a repeater field in acf to save the form submitted values in the database and assign it to Cf7 Submissions options page

4. CF7 provides hook wpcf7_before_send_mail which enable us to process the  submiited data the way we like before submitting the form. I am using this hook to save the form submission in the dashboard

5. update_field updates the values in the database
We are capturing the previously submitted data, so that we could merge it with the new submitted form values .

Note : The previously saved data’s in the repeater fields are lost if we don’t merge and update it with the new data’s submitted by the form

6.  Check the cf7 submission options page to see the lists of saved submitted form fields in the dashboard

 

Hits: 3

Standard
Filter and Hooks

PHP code to Import the POST from one website to another

1. Create a folder in the root directory of the site i.e PARENT_SITE. I have created services folder in the root directory and created the php file named news.php

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

3. Write a code that encode all the posts/contents related to ‘post_type’=>’post’. We can replace ‘post_type’ => ‘post’ by any other custom post types

We can check the encoded post contents in the Url : http://PARENT_SITE/services/news.php

4. Create a folder in the root directory of the site, I call it AS CHILD_SITE where we want to import the all the contents of posts from the PARENT_SITE. I have created app folder in the root directory of CHILD_SITE and created the php file named import-news.php inside the folder:

5.   I have created custom posts and two meta fields , using acf to save the thumbnail  and permalink of imported posts

p.s. I   like to display the thumbnail of news directly from the source instead of saving it in my site and I  might need the source of the  news in the future

6. I have created the field from so that user can input the url from where they can get the json encoded posts to be imported

7.  I have created Importer class that handles the import part. So that I can update the post_type and source link of import  & use the same code in other Projects. To parse the json encoded contents  I have used Guzzle

9. Last but not the list,  run the import file in the CHILD_SITE to import the news  : http://CHILD_SITE/app/import-news.php

P.S. The process is long and I haven’t displayed any screen-shots so that you could try it on your own; provided the full code 😛

Hits: 9

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
WordPress

Filter to add new css class to li elements

Most of us are familiar with adding class from the dashboard that follows :

1. In Appearance > Menus, click the screen Option Tab

2. Under Show advanced menu properties, check CSS Classes

3. Expand any menu item to see the css class text input field

4. Enter desired class name and save menu to apply the class to the menu item

If you are searching for an option to get rid of editing each menu items to add same custom class to all in the menu items, you can go for filter

Where,

  • $classes is an array of the CSS classes that are applied to the menu item’s element,
  • $item is the current menu item,
  • $args is an object of wp_nav_menu() arguments,
  • $depth is Depth of menu item

To learn more about the filter, please can refer the codex.

Hits: 5

Standard