Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * 1. Add custom metabox
- */
- function pm_add_metabox() {
- add_meta_box(
- 'bis_additional-data',
- __( 'Query URL Parameters', 'bis' ),
- 'bis_mb_html',
- array('page'),
- 'side',
- 'high'
- );
- }
- add_action( 'add_meta_boxes', 'pm_add_metabox' );
- function bis_mb_html($post) {
- wp_nonce_field( '_pm_metabox_nonce', 'pm_metabox_nonce' );
- $pm_url_parameters = esc_textarea(get_post_meta(get_the_ID(), 'pm_url_parameters', true));
- ?>
- <p>
- <input class="widefat" name="pm_url_parameters" type="text" value="<?php echo esc_attr( $pm_url_parameters ); ?>">
- </p>
- <?php
- }
- /**
- * 2. Save custom metabox data
- */
- function pm_metabox_save($post_id) {
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; }
- if ( ! isset( $_POST['pm_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['pm_metabox_nonce'], '_pm_metabox_nonce' ) ) { return; }
- if ( ! current_user_can( 'edit_post', $post_id ) ) { return; }
- if ( isset( $_POST['pm_url_parameters'] ) || isset( $_POST['pm_url_parameters'] ) ) {
- update_post_meta( $post_id, 'pm_url_parameters', $_POST['pm_url_parameters'] );
- }
- }
- add_action( 'save_post', 'pm_metabox_save' );
- /**
- * 3. Add query URL parameters to the custom permalink
- */
- function pm_append_url_parameter($permalink, $post) {
- if(!empty($post->post_type) && (in_array($post->post_type, array('page')))) {
- $pm_url_parameters = esc_textarea(get_post_meta(get_the_ID(), 'pm_url_parameters', true));
- if(!empty($pm_url_parameters)) {
- $permalink .= $pm_url_parameters;
- }
- }
- return $permalink;
- }
- add_filter('permalink_manager_filter_final_post_permalink', 'pm_append_url_parameter', 999, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement