Skip to content

Instantly share code, notes, and snippets.

@horike37
Last active April 12, 2016 07:03
Show Gist options
  • Save horike37/7298d8b9ce461479b235 to your computer and use it in GitHub Desktop.
Save horike37/7298d8b9ce461479b235 to your computer and use it in GitHub Desktop.
親サイト(blog_idが1)に作ったTrust Formのフォームを新規サイト作成時にコピーして、自動で固定ページにフォームを設置するコード
<?php
add_action( 'wpmu_new_blog', function($blog_id) {
//マルチサイトの親のお問い合わせデータを取ってくる
switch_to_blog(1);
$trust_form = get_posts( array('post_type'=>'trust-form') );
if ( empty($trust_form) ) {
return;
}
foreach ( $trust_form as $t ) {
$form_id = $t->ID;
}
$post = get_post( $form_id );
if ( empty($post) ) {
return;
}
$new = array(
'post_author' => $post->post_author,
'post_date' => $post->post_date,
'post_date_gmt' => $post->post_date_gmt,
'post_content' => $post->post_content,
'post_title' => $post->post_title,
'post_excerpt' => $post->post_excerpt,
'post_status' => $post->post_status,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_password' => $post->post_password,
'post_name' => $post->post_name,
'to_ping' => $post->to_ping,
'pinged' => $post->pinged,
'post_modified' => $post->post_modified,
'post_modified_gmt' => $post->post_modified_gmt,
'post_content_filtered' => $post->post_content_filtered,
'post_parent' => $post->post_parent,
'guid' => $post->guid,
'menu_order' => $post->menu_order,
'post_type' => $post->post_type,
'post_mime_type' => $post->post_mime_type,
'comment_count' => $post->comment_count
);
$data = array(
'name' => get_post_meta( $form_id, 'name', true ),
'attention' => get_post_meta( $form_id, 'attention', true ),
'type' => get_post_meta( $form_id, 'type', true ),
'validation' => get_post_meta( $form_id, 'validation', true ),
'attr' => get_post_meta( $form_id, 'attr', true ),
'admin_mail' => get_post_meta( $form_id, 'admin_mail', true ),
'user_mail' => get_post_meta( $form_id, 'user_mail', true ),
'form_admin_input' => get_post_meta( $form_id, 'form_admin_input', true ),
'form_admin_confirm' => get_post_meta( $form_id, 'form_admin_confirm', true ),
'form_admin_finish' => get_post_meta( $form_id, 'form_admin_finish', true ),
'form_front' => get_post_meta( $form_id, 'form_front', true ),
'config' => get_post_meta( $form_id, 'config', true ),
'other_setting' => get_post_meta( $form_id, 'other_setting', true )
);
switch_to_blog($blog_id);
//新規作成したマルチサイトにコピーしたフォームを複製
if ( !$new_id = wp_insert_post($new) ) {
return;
}
//フォームのカスタムフィールドをコピー
update_post_meta( $new_id, 'name', $data['name'] );
update_post_meta( $new_id, 'attention', $data['attention'] );
update_post_meta( $new_id, 'type', $data['type'] );
update_post_meta( $new_id, 'validation', $data['validation'] );
update_post_meta( $new_id, 'attr', $data['attr'] );
update_post_meta( $new_id, 'admin_mail', $data['admin_mail'] );
update_post_meta( $new_id, 'user_mail', $data['user_mail'] );
update_post_meta( $new_id, 'form_admin_input', $data['form_admin_input'] );
update_post_meta( $new_id, 'form_admin_confirm', $data['form_admin_confirm'] );
update_post_meta( $new_id, 'form_admin_finish', $data['form_admin_finish'] );
update_post_meta( $new_id, 'form_front', $data['form_front'] );
update_post_meta( $new_id, 'config', $data['config'] );
update_post_meta( $new_id, 'other_setting', $data['other_setting'] );
//固定ページにお問い合わせのショートコード埋め込む
$new_page = array(
'post_content' => '[trust-form id='.$new_id.']',
'post_title' => 'お問い合わせ',
'post_type' => 'page',
'post_status' => 'publish'
);
wp_insert_post($new_page);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment