Every wordpress developers should agree that Advanced custom fields (ACF) has reduced development time and it gave more flexibility to extend wordpress custom fields functionality. ACF also make the wordpress backend more user friendly.
In this article am going to show how we can build a settings page for google analytics using ACF. This can done in two ways.
- Page based approach – Using free version of ACF
- ACF options – Using ACF PRO or ACF addon ACF Options
I will show how we can build using ACF options addon. To proceed further first install ACF core plugin and activate ACF options or else install ACF Pro.
Step 1: Create settings page
Add this code in functions.php. It will enable google analytics settings in wp-admin
'page_title' => 'Google Analytics',
'menu_title' => 'Google Analytics',
'menu_slug' => 'theme-analytics-settings',
'capability' => 'edit_posts',
'redirect' => false
Step 2: Add fields to settings page
Login to backend and create new custom field group. Set the target to options page and publish the page and your settings page is ready.
Go to newly created settings page and paste your copied google analytics tracking code.
Step 3: Retrieve google analytics in front end
Add this piece of code to functions.php. This will retrieve saved google analytics in frontend.
$google_cont = get_field('analytics_code','option');
That’s all we are done with settings page using ACF. Try it out. Feedback are welcome.