feat: add keywords field to articles
Introduced a new field to allow the addition of keywords to articles. Updated the meta box functions to include a UI for managing keywords in the article editor. Enhanced the save functionality to handle the new keywords field, ensuring data is stored securely. This change enables better content categorization and searchability by allowing authors to specify relevant keywords.
This commit is contained in:
parent
744f31477d
commit
e7a4f5923b
2 changed files with 62 additions and 3 deletions
|
@ -10,6 +10,7 @@ It includes:
|
|||
- Latest articles on the homepage
|
||||
- Field to list authors
|
||||
- Lead author "et al." is listed on the index and archive pages
|
||||
- Field to list keywords
|
||||
- Field for "DOI" (Digital Object Identifier)
|
||||
- Generator for DOI
|
||||
- Prefix configurable in Customizer (please change this to your own prefix)
|
||||
|
|
|
@ -515,6 +515,59 @@ function render_article_open_access_meta_box($post)
|
|||
<?php
|
||||
}
|
||||
|
||||
function add_keywords_meta_box()
|
||||
{
|
||||
add_meta_box(
|
||||
'article_keywords',
|
||||
__('Article Keywords', 'duck-behavior-journal'),
|
||||
'render_keywords_meta_box',
|
||||
'article',
|
||||
'side',
|
||||
'default'
|
||||
);
|
||||
}
|
||||
|
||||
add_action('add_meta_boxes', 'add_keywords_meta_box');
|
||||
|
||||
function render_keywords_meta_box($post)
|
||||
{
|
||||
$keywords = get_post_meta($post->ID, 'article_keywords', true);
|
||||
?>
|
||||
<div id="article-keywords">
|
||||
<div id="article-keywords-list">
|
||||
<?php
|
||||
if (!empty($keywords)) {
|
||||
foreach ($keywords as $index => $keyword) {
|
||||
?>
|
||||
<p>
|
||||
<input type="text" name="article_keywords[]" value="<?php echo esc_attr($keyword); ?>" />
|
||||
<button type="button" class="button remove-keyword-button" data-index="<?php echo $index; ?>"><?php _e('Remove', 'duck-behavior-journal'); ?></button>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
} else {
|
||||
?>
|
||||
<p><input type="text" name="article_keywords[]" value="" /></p>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<button type="button" id="add-keyword-button" class="button"><?php _e('Add Keyword', 'duck-behavior-journal'); ?></button>
|
||||
</div>
|
||||
<script>
|
||||
jQuery(document).ready(function($) {
|
||||
$('#add-keyword-button').on('click', function() {
|
||||
$('#article-keywords-list').append('<p><input type="text" name="article_keywords[]" value="" /><button type="button" class="button remove-keyword-button"><?php _e('Remove', 'duck-behavior-journal'); ?></button></p>');
|
||||
});
|
||||
|
||||
$(document).on('click', '.remove-keyword-button', function() {
|
||||
$(this).parent().remove();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
function save_article_meta_boxes($post_id)
|
||||
{
|
||||
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
|
||||
|
@ -533,6 +586,11 @@ function save_article_meta_boxes($post_id)
|
|||
update_post_meta($post_id, 'article_attachment_id', $attachment_id);
|
||||
}
|
||||
|
||||
if (isset($_POST['article_keywords'])) {
|
||||
$keywords = array_map('sanitize_text_field', $_POST['article_keywords']);
|
||||
update_post_meta($post_id, 'article_keywords', $keywords);
|
||||
}
|
||||
|
||||
$open_access = isset($_POST['article_open_access']) ? '1' : '0';
|
||||
update_post_meta($post_id, 'article_open_access', $open_access);
|
||||
}
|
||||
|
@ -1128,7 +1186,7 @@ function add_article_columns($columns)
|
|||
|
||||
// Remove the default author column
|
||||
unset($columns['author']);
|
||||
|
||||
|
||||
return $columns;
|
||||
}
|
||||
add_filter('manage_article_posts_columns', 'add_article_columns');
|
||||
|
@ -1155,7 +1213,7 @@ function add_letter_columns($columns)
|
|||
|
||||
// Remove the default author column
|
||||
unset($columns['author']);
|
||||
|
||||
|
||||
return $columns;
|
||||
}
|
||||
add_filter('manage_letter_posts_columns', 'add_letter_columns');
|
||||
|
@ -1172,4 +1230,4 @@ function custom_letter_column($column, $post_id)
|
|||
}
|
||||
}
|
||||
}
|
||||
add_action('manage_letter_posts_custom_column', 'custom_letter_column', 10, 2);
|
||||
add_action('manage_letter_posts_custom_column', 'custom_letter_column', 10, 2);
|
||||
|
|
Loading…
Reference in a new issue