php Popuplate使用数据库中存在的所有可用重力形式的ACF选择字段命名为“gravityform”。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Popuplate使用数据库中存在的所有可用重力形式的ACF选择字段命名为“gravityform”。相关的知识,希望对你有一定的参考价值。
<?php
/**
* Migrates the content from the old database to WP
*
* @package dtg
*/
/**
* Migrate the NEWS
*
* @return void
*/
function dtg_migrate_news() {
global $wpdb;
// Get the news from the old website (that haven't been imported yet).
$results = $wpdb->get_results( "
SELECT * FROM dtg_live_dtgnews.stories WHERE story_id NOT IN (
SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_story_id'
)
" );
foreach ( $results as $result ) {
$data = array(
'post_title' => $result->headline,
'post_content' => $result->body,
'post_date' => $result->date_filed,
'post_modified' => $result->date_modified,
'post_status' => 'publish',
);
$post_id = wp_insert_post( $data );
// Upload the attachment.
if ( ! empty( $result->image_name ) ) {
$image = ABSPATH . '/wp-content/imagestmp/' . $result->image_type . '/' . $result->image_name;
if ( ! file_exists( $image ) ) {
var_dump( $image );
die();
}
nine3_upload_image( $image, $post_id );
}
// The story ID and author byline.
update_post_meta( $post_id, '_story_id', $result->story_id );
update_post_meta( $post_id, 'byline', $result->byline );
// Custom Links.
for ( $i = 1; $i <= 6; $i++ ) {
if ( ! empty( $result->{'link_more_' . $i} ) ) {
update_post_meta( $post_id, 'link_more_' . $i, $result->{'link_more_' . $i} );
update_post_meta( $post_id, 'link_caption_' . $i, $result->{'link_caption_' . $i} );
}
}
// Custom linked news.
for ( $i = 1; $i <= 2; $i++ ) {
if ( ! empty( $result->{'link_news' . $i} ) ) {
update_post_meta( $post_id, 'link_news_' . $i, $result->{'link_news_' . $i} );
update_post_meta( $post_id, 'link_news_caption' . $i, $result->{'link_news_' . $i . '_caption'} );
}
}
}
echo 'done';
die();
}
/**
* Upload the image into the media library and set it as featured image for $post_id (if specified).
*
* @param string $url the image url/path.
* @param int $post_id the post it to whom assign the featured image.
*
* @return int the attachment id.
*/
function nine3_upload_image( $url, $post_id = false ) {
// Use file_get_contents for file saved locally and wp_remote_get for the remote ones.
if ( stripos( $url, 'http' ) === 0 ) {
$content = file_get_contents( $url );
} else {
$content = wp_remote_get( $url );
}
// Set the upload date as the post date.
$post_date = $post_id ? get_post_field( 'post_date', $post_id ) : null;
$filename = basename( $url );
$upload_file = wp_upload_bits( $filename, '', $content, $post_date );
if ( ! $upload_file['error'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => (int) $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit',
);
if ( ! empty( $post_date ) ) {
$attachment['post_date'] = $post_date;
}
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 0 );
if ( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
return $attachment_id;
}
}
return null;
}
/**
* Which migrate action to run?
*/
if ( isset( $_GET['migrate'] ) ) {
$action = sanitize_title( $_GET['migrate'] );
add_filter( 'admin_head', 'dtg_migrate_' . $action );
}
<?php
/**
* Migrates the content from the old database to WP
*
* @package dtg
*/
/**
* Migrate the NEWS
*
* @return void
*/
function dtg_migrate_news() {
global $wpdb;
// Get the news from the old website (that haven't been imported yet).
$results = $wpdb->get_results( "
SELECT * FROM dtg_live_dtgnews.stories WHERE story_id NOT IN (
SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_story_id'
)
" );
foreach ( $results as $result ) {
$data = array(
'post_title' => $result->headline,
'post_content' => $result->body,
'post_date' => $result->date_filed,
'post_modified' => $result->date_modified,
'post_status' => 'publish',
);
$post_id = wp_insert_post( $data );
// Upload the attachment.
if ( ! empty( $result->image_name ) ) {
$image = ABSPATH . '/wp-content/imagestmp/' . $result->image_type . '/' . $result->image_name;
if ( ! file_exists( $image ) ) {
var_dump( $image );
die();
}
nine3_upload_image( $image, $post_id );
}
// The story ID and author byline.
update_post_meta( $post_id, '_story_id', $result->story_id );
update_post_meta( $post_id, 'byline', $result->byline );
// Custom Links.
for ( $i = 1; $i <= 6; $i++ ) {
if ( ! empty( $result->{'link_more_' . $i} ) ) {
update_post_meta( $post_id, 'link_more_' . $i, $result->{'link_more_' . $i} );
update_post_meta( $post_id, 'link_caption_' . $i, $result->{'link_caption_' . $i} );
}
}
// Custom linked news.
for ( $i = 1; $i <= 2; $i++ ) {
if ( ! empty( $result->{'link_news' . $i} ) ) {
update_post_meta( $post_id, 'link_news_' . $i, $result->{'link_news_' . $i} );
update_post_meta( $post_id, 'link_news_caption' . $i, $result->{'link_news_' . $i . '_caption'} );
}
}
}
echo 'done';
die();
}
/**
* Upload the image into the media library and set it as featured image for $post_id (if specified).
*
* @param string $url the image url/path.
* @param int $post_id the post it to whom assign the featured image.
*
* @return int the attachment id.
*/
function nine3_upload_image( $url, $post_id = false ) {
// Use file_get_contents for file saved locally and wp_remote_get for the remote ones.
if ( stripos( $url, 'http' ) === 0 ) {
$content = file_get_contents( $url );
} else {
$content = wp_remote_get( $url );
}
// Set the upload date as the post date.
$post_date = $post_id ? get_post_field( 'post_date', $post_id ) : null;
$filename = basename( $url );
$upload_file = wp_upload_bits( $filename, '', $content, $post_date );
if ( ! $upload_file['error'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => (int) $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit',
);
if ( ! empty( $post_date ) ) {
$attachment['post_date'] = $post_date;
}
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 0 );
if ( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
return $attachment_id;
}
}
return null;
}
/**
* Which migrate action to run?
*/
if ( isset( $_GET['migrate'] ) ) {
$action = sanitize_title( $_GET['migrate'] );
add_filter( 'admin_head', 'dtg_migrate_' . $action );
}
<?php
/**
* Migrates the content from the old database to WP
*
* @package dtg
*/
/**
* Migrate the NEWS
*
* @return void
*/
function dtg_migrate_news() {
global $wpdb;
// Get the news from the old website (that haven't been imported yet).
$results = $wpdb->get_results( "
SELECT * FROM dtg_live_dtgnews.stories WHERE story_id NOT IN (
SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_story_id'
)
" );
foreach ( $results as $result ) {
$data = array(
'post_title' => $result->headline,
'post_content' => $result->body,
'post_date' => $result->date_filed,
'post_modified' => $result->date_modified,
'post_status' => 'publish',
);
$post_id = wp_insert_post( $data );
// Upload the attachment.
if ( ! empty( $result->image_name ) ) {
$image = ABSPATH . '/wp-content/imagestmp/' . $result->image_type . '/' . $result->image_name;
if ( ! file_exists( $image ) ) {
var_dump( $image );
die();
}
nine3_upload_image( $image, $post_id );
}
// The story ID and author byline.
update_post_meta( $post_id, '_story_id', $result->story_id );
update_post_meta( $post_id, 'byline', $result->byline );
// Custom Links.
for ( $i = 1; $i <= 6; $i++ ) {
if ( ! empty( $result->{'link_more_' . $i} ) ) {
update_post_meta( $post_id, 'link_more_' . $i, $result->{'link_more_' . $i} );
update_post_meta( $post_id, 'link_caption_' . $i, $result->{'link_caption_' . $i} );
}
}
// Custom linked news.
for ( $i = 1; $i <= 2; $i++ ) {
if ( ! empty( $result->{'link_news' . $i} ) ) {
update_post_meta( $post_id, 'link_news_' . $i, $result->{'link_news_' . $i} );
update_post_meta( $post_id, 'link_news_caption' . $i, $result->{'link_news_' . $i . '_caption'} );
}
}
}
echo 'done';
die();
}
/**
* Upload the image into the media library and set it as featured image for $post_id (if specified).
*
* @param string $url the image url/path.
* @param int $post_id the post it to whom assign the featured image.
*
* @return int the attachment id.
*/
function nine3_upload_image( $url, $post_id = false ) {
// Use file_get_contents for file saved locally and wp_remote_get for the remote ones.
if ( stripos( $url, 'http' ) === 0 ) {
$content = file_get_contents( $url );
} else {
$content = wp_remote_get( $url );
}
// Set the upload date as the post date.
$post_date = $post_id ? get_post_field( 'post_date', $post_id ) : null;
$filename = basename( $url );
$upload_file = wp_upload_bits( $filename, '', $content, $post_date );
if ( ! $upload_file['error'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => (int) $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit',
);
if ( ! empty( $post_date ) ) {
$attachment['post_date'] = $post_date;
}
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 0 );
if ( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
return $attachment_id;
}
}
return null;
}
/**
* Which migrate action to run?
*/
if ( isset( $_GET['migrate'] ) ) {
$action = sanitize_title( $_GET['migrate'] );
add_filter( 'admin_head', 'dtg_migrate_' . $action );
}
<?php
/**
* Migrates the content from the old database to WP
*
* @package dtg
*/
/**
* Migrate the NEWS
*
* @return void
*/
function dtg_migrate_news() {
global $wpdb;
// Get the news from the old website (that haven't been imported yet).
$results = $wpdb->get_results( "
SELECT * FROM dtg_live_dtgnews.stories WHERE story_id NOT IN (
SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_story_id'
)
" );
foreach ( $results as $result ) {
$data = array(
'post_title' => $result->headline,
'post_content' => $result->body,
'post_date' => $result->date_filed,
'post_modified' => $result->date_modified,
'post_status' => 'publish',
);
$post_id = wp_insert_post( $data );
// Upload the attachment.
if ( ! empty( $result->image_name ) ) {
$image = ABSPATH . '/wp-content/imagestmp/' . $result->image_type . '/' . $result->image_name;
if ( ! file_exists( $image ) ) {
var_dump( $image );
die();
}
nine3_upload_image( $image, $post_id );
}
// The story ID and author byline.
update_post_meta( $post_id, '_story_id', $result->story_id );
update_post_meta( $post_id, 'byline', $result->byline );
// Custom Links.
for ( $i = 1; $i <= 6; $i++ ) {
if ( ! empty( $result->{'link_more_' . $i} ) ) {
update_post_meta( $post_id, 'link_more_' . $i, $result->{'link_more_' . $i} );
update_post_meta( $post_id, 'link_caption_' . $i, $result->{'link_caption_' . $i} );
}
}
// Custom linked news.
for ( $i = 1; $i <= 2; $i++ ) {
if ( ! empty( $result->{'link_news' . $i} ) ) {
update_post_meta( $post_id, 'link_news_' . $i, $result->{'link_news_' . $i} );
update_post_meta( $post_id, 'link_news_caption' . $i, $result->{'link_news_' . $i . '_caption'} );
}
}
}
echo 'done';
die();
}
/**
* Upload the image into the media library and set it as featured image for $post_id (if specified).
*
* @param string $url the image url/path.
* @param int $post_id the post it to whom assign the featured image.
*
* @return int the attachment id.
*/
function nine3_upload_image( $url, $post_id = false ) {
// Use file_get_contents for file saved locally and wp_remote_get for the remote ones.
if ( stripos( $url, 'http' ) === 0 ) {
$content = file_get_contents( $url );
} else {
$content = wp_remote_get( $url );
}
// Set the upload date as the post date.
$post_date = $post_id ? get_post_field( 'post_date', $post_id ) : null;
$filename = basename( $url );
$upload_file = wp_upload_bits( $filename, '', $content, $post_date );
if ( ! $upload_file['error'] ) {
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_parent' => (int) $post_id,
'post_title' => preg_replace( '/\.[^.]+$/', '', $filename ),
'post_content' => '',
'post_status' => 'inherit',
);
if ( ! empty( $post_date ) ) {
$attachment['post_date'] = $post_date;
}
$attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], 0 );
if ( ! is_wp_error( $attachment_id ) ) {
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
return $attachment_id;
}
}
return null;
}
/**
* Which migrate action to run?
*/
if ( isset( $_GET['migrate'] ) ) {
$action = sanitize_title( $_GET['migrate'] );
add_filter( 'admin_head', 'dtg_migrate_' . $action );
}
/**
* Popuplate dinamically the ACF select fields named "gravityform"
* with all the available gravity forms present in the database.
*
* @param array $field the acf parameter.
*/
function nine3_select_gf_forms( $field ) {
global $wpdb;
$sql = "SELECT * FROM {$wpdb->prefix}rg_form";
$forms = $wpdb->get_results( $sql );
$field['choices'] = array();
if ( $forms ) :
foreach ( $forms as $k => $v ) {
$field['choices'][ $v->id ] = apply_filters( 'the_title', $v->title );
}
endif;
return $field;
}
add_filter( 'acf/load_field/name=gravityform', 'bankable_select_gf_forms' );
以上是关于php Popuplate使用数据库中存在的所有可用重力形式的ACF选择字段命名为“gravityform”。的主要内容,如果未能解决你的问题,请参考以下文章