I have this block of code in index.php
<?php
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && isset($_POST['update_post_nonce']) )
{
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) )
{
if (! empty($_POST['post_date'])) {
$post = array(
'ID' => esc_sql($post_id),
'post_date' => esc_sql($_POST['post_date']),
);
wp_update_post($post);
}
if (! empty($_POST['_product_url'])) {
update_post_meta($post_id, '_product_url', $_POST['_product_url']);
}
}
}
?>
what code do I put in functions.php so that in index.php I can just have
blockofcodefunction();
instead of the actual long block of code
Tache Madalin answers:
I think you should do it like this:
<?php
function mad_up() {
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && isset($_POST['update_post_nonce']) ) {
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) ) {
if (! empty($_POST['post_date'])) {
$post = array(
'ID' => esc_sql($post_id),
'post_date' => esc_sql($_POST['post_date']),
);
wp_update_post($post);
}
if (! empty($_POST['_product_url'])) {
update_post_meta($post_id, '_product_url', $_POST['_product_url']);
}
}
}
}
?>
And then just use the mad_up();
Katie comments:
thanks used this
Tache Madalin comments:
You're welcome! Can you award the prize ? Thank you :)
Arnav Joy answers:
actually you can remove whole code and put in functions.php , no need to call anything.
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ! empty($_POST['post_id']) && isset($_POST['update_post_nonce']) )
{
$post_id = $_POST['post_id'];
$post_type = get_post_type($post_id);
$capability = ( 'page' == $post_type ) ? 'edit_page' : 'edit_post';
if ( current_user_can($capability, $post_id) && wp_verify_nonce( $_POST['update_post_nonce'], 'update_post_'. $post_id ) )
{
if (! empty($_POST['post_date'])) {
$post = array(
'ID' => esc_sql($post_id),
'post_date' => esc_sql($_POST['post_date']),
);
wp_update_post($post);
}
if (! empty($_POST['_product_url'])) {
update_post_meta($post_id, '_product_url', $_POST['_product_url']);
}
}
}
Arnav Joy comments:
because I think you are calling any form and when you hit any button , submit or any thing then this code executes.