Skip to content

Instantly share code, notes, and snippets.

@przor3n
Created February 13, 2017 22:22
Show Gist options
  • Save przor3n/82fcf8ef1786caf66c15cbc0e74a65e1 to your computer and use it in GitHub Desktop.
Save przor3n/82fcf8ef1786caf66c15cbc0e74a65e1 to your computer and use it in GitHub Desktop.
Wordpress snips
<?php
function removeShit()
{
global $wpdb;
$posts = $wpdb->get_results("
SELECT wp.ID, wp.post_content FROM wp_posts wp, novationstudio_events_event e WHERE wp.post_title = e.title;
");
foreach($posts as $post) {
$post->post_content = str_replace("center;", "justify;", $post->post_content);
$post->post_content = html_entity_decode($post->post_content);
$post->post_content = trim(preg_replace('/\s\s+/', ' ', $post->post_content));
echo $post->post_content;
$result = $wpdb->update($wpdb->posts, [
'post_content' => $post->post_content
],[
'ID' => $post->ID
]);
var_dump($result);
}
}
function get_event_id($post_title) {
global $wpdb;
$posts = $wpdb->get_results(
"SELECT ei.image, ei.is_thumbnail
FROM novationstudio_events_event e, novationstudio_events_eventimage ei
WHERE ei.event_id = e.id AND e.title = '" . $post_title . "'");
$pcount = count($posts);
for ($i = 0; $i < $pcount; $i++) {
$posts[$i]->image = ltrim($posts[$i]->image, "/");
if (stripos($posts[$i]->image, "media/") === FALSE) {
$posts[$i]->image = "media/" . $posts[$i]->image;
}
}
return $posts;
}
function getOldImages($post_title) {
$images = [];
$files = get_event_id($post_title);
$wp_upload_dir = wp_upload_dir();
foreach ($files as $file) {
$filePath = $wp_upload_dir['basedir'] . "/" . $file->image;
if (file_exists($filePath)) {
$images[] = $wp_upload_dir['baseurl'] . "/" . $file->image;
}
}
return $images;
}
function getOldThumb($post_title) {
$thumb = '';
$files = get_event_id($post_title);
$wp_upload_dir = wp_upload_dir();
foreach ($files as $file) {
if ($file->is_thumbnail) {
$filePath = $wp_upload_dir['basedir'] . "/" . $file->image;
if (file_exists($filePath)) {
$thumb = $wp_upload_dir['baseurl'] . "/" . $file->image;
}
}
}
return $thumb;
}
function saveOldAttachments($parent_post_id, $post_title) {
$files = get_event_id($post_title);
$thumbID = null;
$attachmentIDS = [];
foreach ($files as $file) {
$filename = $file->image;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$source = ABSPATH . $filename;
$dest = $wp_upload_dir['basedir'] . $wp_upload_dir['subdir']. '/' . basename( $filename );
move_uploaded_file($source, $dest);
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail($parent_post_id, $attach_id);
if ($file->is_thumbnail) {
$thumbID = $attach_id;
}
$attachmentIDS[] = (string)$attach_id;
}
foreach ($files as $file) {
if ($file->is_thumbnail) {
set_post_thumbnail($parent_post_id, $thumbID);
}
}
$galeryIDS = json_encode($attachmentIDS);
global $wpdb;
$gallery = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE post_id = {$parent_post_id} AND meta_key = '_gallery'" );
if (is_null($gallery)) {
$result = $wpdb->insert($wpdb->postmeta, [
"post_id" => $parent_post_id,
"meta_key" => "_gallery",
"meta_value" => $galeryIDS],
["%d", "%s", "%s"]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment