Created
August 9, 2019 07:18
-
-
Save johnjullies/4291b2965f374902964b4847e35a1f71 to your computer and use it in GitHub Desktop.
Custom WordPress function - coding exam submission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A wordpress function that triggers #publish button onClick | |
* when on a post page (admin); then: | |
* 1) gets all custom_fields of an extended Pod that are marked Required, of the current post_type | |
* 2) checks if all the required fields have a value, if not, throws an error | |
* 3) checks if custom_field XYZ has a value between 50 & 80, if not throws an error | |
* 4) changes post_status to "publish" | |
* 5) re-directs to homepage. | |
*/ | |
function my_custom_function() { | |
// check if on a post page | |
$post_type = get_post_type(); | |
if ($post_type != null) : | |
// trigger onClick | |
?> | |
<script type="text/javascript"> | |
(function($) { | |
$('#publish').onClick(function() { | |
<?php | |
// get all custom_fields of an extended pod that are marked Required | |
// of current post_type | |
$pod = pods( 'mypod', $post_type ); | |
$fields = $pod->fields(); | |
$required = array_filter( $fields, function( $item ) { | |
return ( $item["options"]["required"] == "1" ); | |
} ); | |
// check if all of them has a value else throw an error | |
while ( $pod->fetch() ) { | |
if ( $pod->display( 'name' ) == "" ) { | |
wp_die( "Error!" ); | |
} | |
} | |
// check if 50 <= custom_field XYZ <= 80 else throw an error | |
$field = $pod->fields( 'custom_field' ); | |
if ( $field < 50 || $field > 80 ) { | |
wp_die( "Error!" ); | |
} | |
// change post_status to "publish" | |
$post = get_post(); | |
wp_update_post( $post ); | |
// redirect to homepage | |
wp_redirect( '/' ); | |
exit; | |
?> | |
}); | |
})(jQuery); | |
</script> | |
<?php | |
endif; | |
} | |
add_action( 'admin_footer', 'my_custom_function' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment