0

I've written a Wordpress calendar plugin and would like to put an "Add Post" link on specified days (i.e., those without posts):

Preview

When I click the link, I'd like to be taken to the "Add New Post" admin page with that particular calendar date pre-selected as the post publish date. Is there some way to do this via query string in a URL?

For example: http://example.com/wp-admin/post-new.php?post_date=2016-09-14

Is this way somehow workable? Is there a different method I can try to pass the date to the "Add New Post" page? Am I just dreaming? :) TIA!

  • You could set the value of the post date field(s) with Javascript/jQuery. Use the admin_head or admin_footer or admin_enqueue_scripts hook to put the JS into admin page. You use a URL parameter like in your question and the write the JS to detect if the URL parameter exists and then insert it into the inputs. – Dan. Nov 29 '16 at 22:14
  • You can add a form with a hidden field, then redirect and on the redirected page, check the $_POST array. – kaiser Nov 29 '16 at 23:05
  • Thank you both so much -- you pointed me in the right direction and I was able to get it working. I posted my solution below! – Angela T Dec 2 '16 at 3:25
0

Here is the solution I came up with!

In my plugin functions:

<?php
function ficma_inline_script() {
    $datedata = explode("-", $_POST['date']);
    $year = $datedata[0];
    $month = $datedata[1];
    $day = $datedata[2];
    $newdate = date("M d, Y", mktime(0, 0, 0, $month, $day, $year));
?>
<script type="text/javascript">
document.getElementById("mm").value = "<?=$month ?>";
document.getElementById("cur_mm").value = "<?=$month ?>";
document.getElementById("hidden_mm").value = "<?=$month ?>";
document.getElementById("jj").value = "<?=$day ?>";
document.getElementById("cur_jj").value = "<?=$day ?>";
document.getElementById("hidden_jj").value = "<?=$day ?>";
document.getElementById("aa").value = "<?=$year ?>";
document.getElementById("cur_aa").value = "<?=$year ?>";
document.getElementById("hidden_aa").value = "<?=$year ?>";
document.getElementById("timestamp").innerHTML = "Publish date: <b><?=$newdate ?></b>";
document.getElementById("title").value = "<?=$titledate ?>";
alert("Script loaded inline! <?=$newdate ?> / <?=$titledate ?>");
</script>
<?php
}

global $pagenow;
if (!empty($pagenow) && 'post-new.php' === $pagenow && isset($_POST['date']) && $_POST['date'] != "") {
    add_action('admin_footer', 'ficma_inline_script');
}
?>

And in my content:

<form id="<?=$formname ?>" method="post" action="<?=admin_url() ?>post-new.php" target="_blank" class="ficma-form">
    <input type="hidden" name="date" value="<?=$caldate ?>" />
    <input type="submit" name="submit" value="+" class="ficma-form-submit" style="<?=$ficma_color_add_post_link ?>" />
</form>

The key was loading the JS in the admin footer so the field values were updated after the form fully loaded.

It was also important to set the values of the hidden_* fields. Without changing them, if you clicked the Edit link to change the publish date, then canceled, the values would reset to the current date.

*Edited 24 April 2017: Added some JS-changed values. Custom date is not preserved when Save as Draft, Save as Pending, or Publish button is clicked, however; search for a fix is underway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.