Created
May 28, 2024 20:28
-
-
Save alexstandiford/873dfb333841e38b08bf43d75007379c to your computer and use it in GitHub Desktop.
Quick Post Widget
This file contains 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 | |
/* | |
Plugin Name: Quick Post Widget | |
Description: Adds a Quick Post widget that creates and publishes posts with hashtags converted to tags. | |
*/ | |
// Add the Quick Post widget to the dashboard | |
function qpw_add_dashboard_widgets() { | |
wp_add_dashboard_widget('qpw_widget', 'Quick Post', 'qpw_widget_display'); | |
} | |
add_action('wp_dashboard_setup', 'qpw_add_dashboard_widgets'); | |
// Display the widget | |
function qpw_widget_display() { | |
?> | |
<form method="post" action=""> | |
<textarea name="qpw_content" rows="5" style="width:100%; white-space: pre-wrap;"></textarea> | |
<input type="submit" class="button button-primary" value="Submit"> | |
<?php wp_nonce_field('qpw_nonce_action', 'qpw_nonce_field'); ?> | |
</form> | |
<?php | |
} | |
// Handle form submission | |
function qpw_handle_form_submission() { | |
if (isset($_POST['qpw_content']) && check_admin_referer('qpw_nonce_action', 'qpw_nonce_field')) { | |
$content = sanitize_textarea_field($_POST['qpw_content']); | |
// Extract hashtags and remove them from the content | |
preg_match_all('/#(\w+)/', $content, $matches); | |
$tags = array_map('qpw_process_hashtag', $matches[1]); | |
$content = preg_replace('/#(\w+)/', '', $content); | |
$content = trim($content); | |
// Create the post | |
$post_id = wp_insert_post(array( | |
'post_title' => wp_trim_words(strip_tags($content), 10), | |
'post_content' => qpw_convert_to_paragraph_blocks($content), | |
'post_status' => 'publish' | |
)); | |
// Set the tags for the post | |
wp_set_post_tags($post_id, $tags); | |
// Set the post format to 'aside' | |
set_post_format($post_id, 'aside'); | |
// Redirect to the new post | |
wp_redirect('https://www.alexstandiford.com/micro/' . $post_id); | |
exit; | |
} | |
} | |
add_action('admin_init', 'qpw_handle_form_submission'); | |
// Convert content to paragraph blocks | |
function qpw_convert_to_paragraph_blocks($content) { | |
$lines = explode("\n", $content); | |
$blocks = array_map(function($line) { | |
$line = trim($line); | |
if (!empty($line)) { | |
return "<!-- wp:paragraph --><p>{$line}</p><!-- /wp:paragraph -->"; | |
} | |
return ''; | |
}, $lines); | |
return implode("\n\n", array_filter($blocks)); | |
} | |
// Process hashtags to find or create appropriate tags | |
function qpw_process_hashtag($hashtag) { | |
// Check for the exact match | |
if ($term = get_term_by('name', $hashtag, 'post_tag')) { | |
return $term->name; | |
} | |
// Convert camelCase to normal case (e.g., WordPress) | |
$normal_case = preg_replace('/([a-z])([A-Z])/', '$1 $2', $hashtag); | |
if ($term = get_term_by('name', $normal_case, 'post_tag')) { | |
return $term->name; | |
} | |
// Convert to kebab-case (e.g., wordpress) | |
$kebab_case = sanitize_title_with_dashes($hashtag); | |
if ($term = get_term_by('slug', $kebab_case, 'post_tag')) { | |
return $term->name; | |
} | |
// If no existing tag is found, create a new one | |
$new_tag = wp_insert_term($normal_case, 'post_tag', array('slug' => $kebab_case)); | |
return get_term_by('id', $new_tag['term_id'], 'post_tag')->name; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment