Currently I am working on a website where people could take courses from external websites. On the course page there is a button which would take you to that website. I made a meta_box which should hold an URL and use that URL for the button, it looks like this: link to form picture
But right now it does not take in this URL which I submit in this form. What part am I doing wrong? Here's my code:
This is in functions.php:
add_action('add_meta_boxes', 'mp_add_custom_metabox');
function mp_meta_callback( $post ) {
echo "Add the link for the course button here:<br/>";
echo "<form method='get'>";
echo "<input type='text' name='buttonurl' placeholder='add a URL here'>";
echo "<input type='submit' value='Add'>";
echo "</form>";
}
This is in the course single page:
<a href="<?php $_GET['buttonurl'];?>">
<p>
<div class="btn btn-primary take-course">
Go to the course!
</div>
</p>
</a>
Thank you in advance for the help!
Try like this:
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );
}
function cd_meta_box_cb()
{
global $post;
$values = get_post_custom( $post->ID );
$button = isset( $values['buttonurl'] ) ? $values['buttonurl'] : '';?>
<p>
<label for="my_meta_box_text">Add the link for the course button here:</label>
<input type='text' name='buttonurl' placeholder='add a URL here' value='<?php echo $button[0];?>'>
</p> <?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Make sure your data is set before trying to save it
if( isset( $_POST['buttonurl'] ) )
update_post_meta( $post_id, 'buttonurl', esc_url( $_POST['buttonurl']) );
}
You can then use the value in frontend like:
$button=get_post_meta( $post_id, 'buttonurl', true );
<a href="<?php echo $button;?>">
<p>
<div class="btn btn-primary take-course">
Go to the course!
</div>
</p>
</a>
Take a reference to : https://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.