Home › Forums › ACF PRO › ACF with Social Article › Reply To: ACF with Social Article
Hi @topy
Ok, so the steps would be as follows:
1) create the form group with all the fields you want to be on your post
2) create a page in wordpress where this form will ‘live’
3) open your functions.php and add these 2 bits of code:
/*---------- User Info ----------*/
// this creates a global 'USERID' which you can use to return the ID of the current user anywhere in your site
if (is_user_logged_in()) {
$current_user = wp_get_current_user();
define('USERID', $current_user->ID);
}
/*---------- Check if I am an admin ----------*/
function is_administrator() {
$current_user = wp_get_current_user();
$roles = $current_user->roles;
return (in_array('administrator', $roles)) ? true : false;
}
4) create a new page template to apply to this page (assuming you know how to do this?)
5) in that template, put this at the very top of your file:
<?php
/**
* Is something being edited?
* Is the user trying to do this, the author of the post being edited?
* If not, redirect back to home
*/
if (isset($_GET['edit'])) {
// get the post being edited
$thispost = get_post($_GET['edit']);
if (USERID == $thispost->post_author || is_administrator()) {
$new_post = $_GET['edit'];
} else {
header('Location: '.get_bloginfo('home'));
exit;
}
} else {
$new_post = 'new_post';
}
/* Template Name: My Post Create Page */
acf_form_head();
get_header();
?>
You will use this page for both post creation, and post editing. To edit the post, just add ?edit=23
to the URL and that will let you edit post 23. Note that the above function will only let the author of post 23, or the administrator edit this post. Anyone else will be redirected to the home page
6) Where you want the edit form to appear, put this code:
<?php
$formoptions = array(
'post_title' => true,
'post_content' => false,
'post_id' => $new_post,
'new_post' => array(
'post_type' => 'insights', // change this to the post type you need
'post_status' => 'publish' // do you want the post to be published immediately? otherwise change this to 'draft'
),
'field_groups' => array(999999), // change this number to the ID of your field group
'submit_value' => 'Publish',
'return' => get_permalink(142), // change this ID to the page you want to send the user back to when they've submitted the form - perhaps a thank you page
'uploader' => 'wp'
);
acf_form($formoptions);
?>
—————–
That should do the trick. Give it a go and let me know if you get stuck.
Good luck!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.