Wordpress: Access URL for post which was just created












0















I am using WordPress to create a website which allows users to upload files of hiking paths (gpx (xml) files).



To do this the user access a page called "Create new track" (this is a WordPress page with a specific template). Via a php created form, the user enters the name of the hike, a short description and chooses the file to upload. My code then makes the usual checks on the entered data and chosen file. If all checks are passed, the file is uploaded to the server and a new post is created (adding the entered title and description and file attached to the post).



I want that once the file has been uploaded and the new post has been created then the user accesses a webpage where he can view the newly created hike. I would like this page to be a second WordPress page called "Edit track" which uses a second specific template.



My current plan is that would use $track_ID (see code - this is the ID of the newly created post) and add this to the url of the "Edit track" url in the form of a url parameter. When the "Edit track page is automatically opened after successful creation of the track then the url param is read and the appropriate track can be edited.



My problem is, what code do I need to write such that once the new post is successfully created the "Edit track" page is accessed??



I am completely stumped! I have tried using php and Javascript, but cannot workout how to do this. All ideas welcome!



The frame of my code is attached.



add_shortcode('sut_form', 'sut_form_shortcode');

function sut_form_shortcode() {

if (isset( $_POST['sut_form_create_track_submitted'] ) &&
wp_verify_nonce($_POST['sut_form_create_track_submitted'], 'sut_form_create_track') ) {

// LOTS OF CHECKS ON WHAT HAS BEEN ENTERED
}
else // ALL CHECKS PASSED, SO WE CAN CREATE THE POST
{
$track_data = array(
'post_title' => $sut_track_name,
'post_content' => $sut_track_text,
'post_status' => 'pending',
'post_author' => $current_user->ID,
'post_type' => 'tracks'
);

// Create track post and attach image
if ($track_id = wp_insert_post($track_data)) { // POST CREATED

wp_set_object_terms( $track_id, (int)$_POST['sut_track_category'], 'track_category'); // CATEGORY ASSIGNED TO POST
update_field('field_5bf39d97d1e8d', $movefile['url'], $track_id); // UPLOADED FILE ATTACHED TO POST

// PROBLEM!!! HOW DO I KNOW ACCESS THE URL FOR POST WHICH HAS JUST BEEN CREATED?
}
}
}









share|improve this question

























  • use get_permalink( $track_id );

    – Tamil Selvan C
    Nov 23 '18 at 6:36











  • Hmm. Not sure. I can either use a little javascript once the post was created - but then, how do I pass $track_ID to the Javascript? Or I could use php code, but then, what php code can open a new url?

    – Alastair Green
    Nov 23 '18 at 6:56











  • use header("Location: URL"):

    – Tamil Selvan C
    Nov 23 '18 at 7:05
















0















I am using WordPress to create a website which allows users to upload files of hiking paths (gpx (xml) files).



To do this the user access a page called "Create new track" (this is a WordPress page with a specific template). Via a php created form, the user enters the name of the hike, a short description and chooses the file to upload. My code then makes the usual checks on the entered data and chosen file. If all checks are passed, the file is uploaded to the server and a new post is created (adding the entered title and description and file attached to the post).



I want that once the file has been uploaded and the new post has been created then the user accesses a webpage where he can view the newly created hike. I would like this page to be a second WordPress page called "Edit track" which uses a second specific template.



My current plan is that would use $track_ID (see code - this is the ID of the newly created post) and add this to the url of the "Edit track" url in the form of a url parameter. When the "Edit track page is automatically opened after successful creation of the track then the url param is read and the appropriate track can be edited.



My problem is, what code do I need to write such that once the new post is successfully created the "Edit track" page is accessed??



I am completely stumped! I have tried using php and Javascript, but cannot workout how to do this. All ideas welcome!



The frame of my code is attached.



add_shortcode('sut_form', 'sut_form_shortcode');

function sut_form_shortcode() {

if (isset( $_POST['sut_form_create_track_submitted'] ) &&
wp_verify_nonce($_POST['sut_form_create_track_submitted'], 'sut_form_create_track') ) {

// LOTS OF CHECKS ON WHAT HAS BEEN ENTERED
}
else // ALL CHECKS PASSED, SO WE CAN CREATE THE POST
{
$track_data = array(
'post_title' => $sut_track_name,
'post_content' => $sut_track_text,
'post_status' => 'pending',
'post_author' => $current_user->ID,
'post_type' => 'tracks'
);

// Create track post and attach image
if ($track_id = wp_insert_post($track_data)) { // POST CREATED

wp_set_object_terms( $track_id, (int)$_POST['sut_track_category'], 'track_category'); // CATEGORY ASSIGNED TO POST
update_field('field_5bf39d97d1e8d', $movefile['url'], $track_id); // UPLOADED FILE ATTACHED TO POST

// PROBLEM!!! HOW DO I KNOW ACCESS THE URL FOR POST WHICH HAS JUST BEEN CREATED?
}
}
}









share|improve this question

























  • use get_permalink( $track_id );

    – Tamil Selvan C
    Nov 23 '18 at 6:36











  • Hmm. Not sure. I can either use a little javascript once the post was created - but then, how do I pass $track_ID to the Javascript? Or I could use php code, but then, what php code can open a new url?

    – Alastair Green
    Nov 23 '18 at 6:56











  • use header("Location: URL"):

    – Tamil Selvan C
    Nov 23 '18 at 7:05














0












0








0








I am using WordPress to create a website which allows users to upload files of hiking paths (gpx (xml) files).



To do this the user access a page called "Create new track" (this is a WordPress page with a specific template). Via a php created form, the user enters the name of the hike, a short description and chooses the file to upload. My code then makes the usual checks on the entered data and chosen file. If all checks are passed, the file is uploaded to the server and a new post is created (adding the entered title and description and file attached to the post).



I want that once the file has been uploaded and the new post has been created then the user accesses a webpage where he can view the newly created hike. I would like this page to be a second WordPress page called "Edit track" which uses a second specific template.



My current plan is that would use $track_ID (see code - this is the ID of the newly created post) and add this to the url of the "Edit track" url in the form of a url parameter. When the "Edit track page is automatically opened after successful creation of the track then the url param is read and the appropriate track can be edited.



My problem is, what code do I need to write such that once the new post is successfully created the "Edit track" page is accessed??



I am completely stumped! I have tried using php and Javascript, but cannot workout how to do this. All ideas welcome!



The frame of my code is attached.



add_shortcode('sut_form', 'sut_form_shortcode');

function sut_form_shortcode() {

if (isset( $_POST['sut_form_create_track_submitted'] ) &&
wp_verify_nonce($_POST['sut_form_create_track_submitted'], 'sut_form_create_track') ) {

// LOTS OF CHECKS ON WHAT HAS BEEN ENTERED
}
else // ALL CHECKS PASSED, SO WE CAN CREATE THE POST
{
$track_data = array(
'post_title' => $sut_track_name,
'post_content' => $sut_track_text,
'post_status' => 'pending',
'post_author' => $current_user->ID,
'post_type' => 'tracks'
);

// Create track post and attach image
if ($track_id = wp_insert_post($track_data)) { // POST CREATED

wp_set_object_terms( $track_id, (int)$_POST['sut_track_category'], 'track_category'); // CATEGORY ASSIGNED TO POST
update_field('field_5bf39d97d1e8d', $movefile['url'], $track_id); // UPLOADED FILE ATTACHED TO POST

// PROBLEM!!! HOW DO I KNOW ACCESS THE URL FOR POST WHICH HAS JUST BEEN CREATED?
}
}
}









share|improve this question
















I am using WordPress to create a website which allows users to upload files of hiking paths (gpx (xml) files).



To do this the user access a page called "Create new track" (this is a WordPress page with a specific template). Via a php created form, the user enters the name of the hike, a short description and chooses the file to upload. My code then makes the usual checks on the entered data and chosen file. If all checks are passed, the file is uploaded to the server and a new post is created (adding the entered title and description and file attached to the post).



I want that once the file has been uploaded and the new post has been created then the user accesses a webpage where he can view the newly created hike. I would like this page to be a second WordPress page called "Edit track" which uses a second specific template.



My current plan is that would use $track_ID (see code - this is the ID of the newly created post) and add this to the url of the "Edit track" url in the form of a url parameter. When the "Edit track page is automatically opened after successful creation of the track then the url param is read and the appropriate track can be edited.



My problem is, what code do I need to write such that once the new post is successfully created the "Edit track" page is accessed??



I am completely stumped! I have tried using php and Javascript, but cannot workout how to do this. All ideas welcome!



The frame of my code is attached.



add_shortcode('sut_form', 'sut_form_shortcode');

function sut_form_shortcode() {

if (isset( $_POST['sut_form_create_track_submitted'] ) &&
wp_verify_nonce($_POST['sut_form_create_track_submitted'], 'sut_form_create_track') ) {

// LOTS OF CHECKS ON WHAT HAS BEEN ENTERED
}
else // ALL CHECKS PASSED, SO WE CAN CREATE THE POST
{
$track_data = array(
'post_title' => $sut_track_name,
'post_content' => $sut_track_text,
'post_status' => 'pending',
'post_author' => $current_user->ID,
'post_type' => 'tracks'
);

// Create track post and attach image
if ($track_id = wp_insert_post($track_data)) { // POST CREATED

wp_set_object_terms( $track_id, (int)$_POST['sut_track_category'], 'track_category'); // CATEGORY ASSIGNED TO POST
update_field('field_5bf39d97d1e8d', $movefile['url'], $track_id); // UPLOADED FILE ATTACHED TO POST

// PROBLEM!!! HOW DO I KNOW ACCESS THE URL FOR POST WHICH HAS JUST BEEN CREATED?
}
}
}






php wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 7:50









Julian Stark

1,2681426




1,2681426










asked Nov 23 '18 at 6:34









Alastair GreenAlastair Green

267




267













  • use get_permalink( $track_id );

    – Tamil Selvan C
    Nov 23 '18 at 6:36











  • Hmm. Not sure. I can either use a little javascript once the post was created - but then, how do I pass $track_ID to the Javascript? Or I could use php code, but then, what php code can open a new url?

    – Alastair Green
    Nov 23 '18 at 6:56











  • use header("Location: URL"):

    – Tamil Selvan C
    Nov 23 '18 at 7:05



















  • use get_permalink( $track_id );

    – Tamil Selvan C
    Nov 23 '18 at 6:36











  • Hmm. Not sure. I can either use a little javascript once the post was created - but then, how do I pass $track_ID to the Javascript? Or I could use php code, but then, what php code can open a new url?

    – Alastair Green
    Nov 23 '18 at 6:56











  • use header("Location: URL"):

    – Tamil Selvan C
    Nov 23 '18 at 7:05

















use get_permalink( $track_id );

– Tamil Selvan C
Nov 23 '18 at 6:36





use get_permalink( $track_id );

– Tamil Selvan C
Nov 23 '18 at 6:36













Hmm. Not sure. I can either use a little javascript once the post was created - but then, how do I pass $track_ID to the Javascript? Or I could use php code, but then, what php code can open a new url?

– Alastair Green
Nov 23 '18 at 6:56





Hmm. Not sure. I can either use a little javascript once the post was created - but then, how do I pass $track_ID to the Javascript? Or I could use php code, but then, what php code can open a new url?

– Alastair Green
Nov 23 '18 at 6:56













use header("Location: URL"):

– Tamil Selvan C
Nov 23 '18 at 7:05





use header("Location: URL"):

– Tamil Selvan C
Nov 23 '18 at 7:05












1 Answer
1






active

oldest

votes


















1














Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.



The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:



$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");


It is not too neat but should work.






share|improve this answer


























  • SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

    – Alastair Green
    Nov 23 '18 at 9:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441653%2fwordpress-access-url-for-post-which-was-just-created%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.



The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:



$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");


It is not too neat but should work.






share|improve this answer


























  • SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

    – Alastair Green
    Nov 23 '18 at 9:33
















1














Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.



The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:



$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");


It is not too neat but should work.






share|improve this answer


























  • SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

    – Alastair Green
    Nov 23 '18 at 9:33














1












1








1







Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.



The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:



$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");


It is not too neat but should work.






share|improve this answer















Your function is registered with add_shortcode, so it is executed during page content rendering, it is too late to use header("location... since headers could be already sent to client.



The only solution I can think of, without changing your code and without knowing your entire project is to print a JavaScript snippet, something like this:



$permalink = get_permalink($track_id);
echo("<script>window.location.replace('$permalink');</script>");


It is not too neat but should work.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 10:06

























answered Nov 23 '18 at 8:44









DanieleAlessandraDanieleAlessandra

625511




625511













  • SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

    – Alastair Green
    Nov 23 '18 at 9:33



















  • SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

    – Alastair Green
    Nov 23 '18 at 9:33

















SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

– Alastair Green
Nov 23 '18 at 9:33





SUCCESS! Thanks for this and thanks for explaining about how/when functions are registered with shortcodes. Did not know this. Thank you.

– Alastair Green
Nov 23 '18 at 9:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441653%2fwordpress-access-url-for-post-which-was-just-created%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'