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
ACF, Filter and Hooks, WordPress

Issue : Custom Field Option missing from the WordPress screen options

If you have installed ACF WordPress plugin for creating custom meta boxes, then it removes the the default custom field metaboxes . The main objective of removing the default WordPress custom field is to increase the speed of load time while editing the posts. Also, you shouldn’t need the custom fields if you have ACF plugin installed in your WordPress site. However, if you need to use default WordPress custom field metabox then you can add the following code to your WordPress theme’s functions.php file.

This will solve your  custom field Option missing issue.

Hits: 13

Standard
Filter and Hooks, Programming, WordPress

Get Menu Items Based on Custom Menu

1. Register your custom navigation menu.
We have readily available funtion to register our custom menu, therefore we do not need to call add_theme_support( ‘menus’ ) :

 

Here, $locations is an associative array of menu location slugs (key) and descriptions (according value). I have registered custom menu ‘primary_menu’ as a key  and ‘Main menu’ is the description for the registered ‘primary_menu’

 

2. Create and add Menu items the dashboard

 

3. Get the array of Menu items

Use the custom menu key ‘primary_menu’ in my case to get the  all the menu items associated with it.

 

4. Function to the get the array of menu items

You can create your own function to get the menu items and its sub-items in an array

 

Hits: 13

Standard
Filter and Hooks, Programming, WordPress

jQuery ajax filtering posts with checkboxes

Let’s say, I have lists of categories in checkbox and I want to filter posts based on the selected categories. I have three conditions for categories filter :

  1.  I want to check “All” checkbox by default and display all the posts accordingly.
  2.  If I check/select the filter other than “All”, the “All” checkbox should be unchecked & the posts of checked categories should be displayed
  3. If I check “All” categories, other selected categories should be unchecked

I begin…

Firstly, list all active categories associated with the posts

Add jQuery that tricks the checkbox as per requirement…

In the above js code,
1. All the checkbox are selected using querySelectorAll

2. var count = document.querySelectorAll(‘.js-filter-checkbox:checked’).length, gives the number of checked categories.

3. If the checkbox is checked and among the checked categories there is no “All”, “ALL’ is unchecked

if(inputElement.checked && inputElement.value != ‘all’){
document.querySelector(‘.js-filter-checkbox[value=”all”]’).checked = false;
}

4. If one of the checked category is “All”,  all the other checked categories are unchecked

if(inputElement.checked && inputElement.value == ‘all’){
document.querySelectorAll(‘.js-filter-checkbox:not([value=”all”])’).forEach(function(item){
item.checked = false;
});
}

5. If non of the categories is checked, “All’ category is checked
if(count == 0) document.querySelector(‘.js-filter-checkbox[value=”all”]’).checked = true;

On page load “All” categories is checked so that all the posts are displayed by default

Ajax code is added to filter the posts based on checked categories

Hook that handles our ajax request is added on function.php  :

Finally, posts are filtered based on the selected categories.

Hits: 829

Standard