Last active
June 6, 2017 16:13
-
-
Save sutandang/3a501b3b21b2a78ab270fb2d959f75a8 to your computer and use it in GitHub Desktop.
how to create default page woocommerce
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
function azweb_marketplace_wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) { | |
global $wpdb; | |
$option_value = get_option( $option ); | |
if ( $option_value > 0 ) { | |
$page_object = get_post( $option_value ); | |
if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) { | |
return $page_object->ID; | |
} | |
} | |
if ( strlen( $page_content ) > 0 ) { | |
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); | |
} else { | |
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); | |
} | |
$valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content ); | |
if ( $valid_page_found ) { | |
if ( $option ) { | |
update_option( $option, $valid_page_found ); | |
} | |
return $valid_page_found; | |
} | |
if ( strlen( $page_content ) > 0 ) { | |
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); | |
} else { | |
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); | |
} | |
if ( $trashed_page_found ) { | |
$page_id = $trashed_page_found; | |
$page_data = array( | |
'ID' => $page_id, | |
'post_status' => 'publish', | |
); | |
wp_update_post( $page_data ); | |
} else { | |
$page_data = array( | |
'post_status' => 'publish', | |
'post_type' => 'page', | |
'post_author' => 1, | |
'post_name' => $slug, | |
'post_title' => $page_title, | |
'post_content' => $page_content, | |
'post_parent' => $post_parent, | |
'comment_status' => 'closed' | |
); | |
$page_id = wp_insert_post( $page_data ); | |
} | |
if ( $option ) { | |
update_option( $option, $page_id ); | |
} | |
return $page_id; | |
} | |
function azweb_marketplace_wc_get_page_id( $page ) { | |
if ( $page == 'pay' || $page == 'thanks' ) { | |
_deprecated_argument( __FUNCTION__, '2.1', 'The "pay" and "thanks" pages are no-longer used - an endpoint is added to the checkout instead. To get a valid link use the WC_Order::get_checkout_payment_url() or WC_Order::get_checkout_order_received_url() methods instead.' ); | |
$page = 'checkout'; | |
} | |
if ( $page == 'change_password' || $page == 'edit_address' || $page == 'lost_password' ) { | |
_deprecated_argument( __FUNCTION__, '2.1', 'The "change_password", "edit_address" and "lost_password" pages are no-longer used - an endpoint is added to the my-account instead. To get a valid link use the wc_customer_edit_account_url() function instead.' ); | |
$page = 'myaccount'; | |
} | |
$page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option('woocommerce_' . $page . '_page_id' ) ); | |
return $page ? absint( $page ) : -1; | |
} | |
function azweb_marketplace_create_woocommerce_default_page() | |
{ | |
$pages = apply_filters( 'woocommerce_create_pages', array( | |
'shop' => array( | |
'name' => _x( 'shop', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'Shop', 'Page title', 'woocommerce' ), | |
'content' => '' | |
), | |
'cart' => array( | |
'name' => _x( 'cart', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'Cart', 'Page title', 'woocommerce' ), | |
'content' => '[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']' | |
), | |
'checkout' => array( | |
'name' => _x( 'checkout', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'Checkout', 'Page title', 'woocommerce' ), | |
'content' => '[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']' | |
), | |
'myaccount' => array( | |
'name' => _x( 'my-account', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'My Account', 'Page title', 'woocommerce' ), | |
'content' => '[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']' | |
) | |
) ); | |
$return = array(); | |
foreach ( $pages as $key => $page ) { | |
$return[] = azweb_marketplace_wc_create_page( esc_sql( $page['name'] ), 'woocommerce_' . $key . '_page_id', $page['title'], $page['content'], ! empty( $page['parent'] ) ? azweb_marketplace_wc_get_page_id( $page['parent'] ) : '' ); | |
} | |
return $return; | |
} | |
echo azweb_marketplace_create_woocommerce_default_page(); |
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
function azweb_marketplace_wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) { | |
global $wpdb; | |
$option_value = get_option( $option ); | |
if ( $option_value > 0 ) { | |
$page_object = get_post( $option_value ); | |
if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) { | |
return $page_object->ID; | |
} | |
} | |
if ( strlen( $page_content ) > 0 ) { | |
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); | |
} else { | |
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); | |
} | |
$valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content ); | |
if ( $valid_page_found ) { | |
if ( $option ) { | |
update_option( $option, $valid_page_found ); | |
} | |
return $valid_page_found; | |
} | |
if ( strlen( $page_content ) > 0 ) { | |
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) ); | |
} else { | |
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); | |
} | |
if ( $trashed_page_found ) { | |
$page_id = $trashed_page_found; | |
$page_data = array( | |
'ID' => $page_id, | |
'post_status' => 'publish', | |
); | |
wp_update_post( $page_data ); | |
} else { | |
$page_data = array( | |
'post_status' => 'publish', | |
'post_type' => 'page', | |
'post_author' => 1, | |
'post_name' => $slug, | |
'post_title' => $page_title, | |
'post_content' => $page_content, | |
'post_parent' => $post_parent, | |
'comment_status' => 'closed' | |
); | |
$page_id = wp_insert_post( $page_data ); | |
} | |
if ( $option ) { | |
update_option( $option, $page_id ); | |
} | |
return $page_id; | |
} | |
function azweb_marketplace_wc_get_page_id( $page ) { | |
if ( $page == 'pay' || $page == 'thanks' ) { | |
_deprecated_argument( __FUNCTION__, '2.1', 'The "pay" and "thanks" pages are no-longer used - an endpoint is added to the checkout instead. To get a valid link use the WC_Order::get_checkout_payment_url() or WC_Order::get_checkout_order_received_url() methods instead.' ); | |
$page = 'checkout'; | |
} | |
if ( $page == 'change_password' || $page == 'edit_address' || $page == 'lost_password' ) { | |
_deprecated_argument( __FUNCTION__, '2.1', 'The "change_password", "edit_address" and "lost_password" pages are no-longer used - an endpoint is added to the my-account instead. To get a valid link use the wc_customer_edit_account_url() function instead.' ); | |
$page = 'myaccount'; | |
} | |
$page = apply_filters( 'woocommerce_get_' . $page . '_page_id', get_option('woocommerce_' . $page . '_page_id' ) ); | |
return $page ? absint( $page ) : -1; | |
} | |
function azweb_marketplace_create_woocommerce_default_page() | |
{ | |
$pages = apply_filters( 'woocommerce_create_pages', array( | |
'shop' => array( | |
'name' => _x( 'shop', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'Shop', 'Page title', 'woocommerce' ), | |
'content' => '' | |
), | |
'cart' => array( | |
'name' => _x( 'cart', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'Cart', 'Page title', 'woocommerce' ), | |
'content' => '[' . apply_filters( 'woocommerce_cart_shortcode_tag', 'woocommerce_cart' ) . ']' | |
), | |
'checkout' => array( | |
'name' => _x( 'checkout', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'Checkout', 'Page title', 'woocommerce' ), | |
'content' => '[' . apply_filters( 'woocommerce_checkout_shortcode_tag', 'woocommerce_checkout' ) . ']' | |
), | |
'myaccount' => array( | |
'name' => _x( 'my-account', 'Page slug', 'woocommerce' ), | |
'title' => _x( 'My Account', 'Page title', 'woocommerce' ), | |
'content' => '[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']' | |
) | |
) ); | |
$return = array(); | |
foreach ( $pages as $key => $page ) { | |
$return[] = azweb_marketplace_wc_create_page( esc_sql( $page['name'] ), 'woocommerce_' . $key . '_page_id', $page['title'], $page['content'], ! empty( $page['parent'] ) ? azweb_marketplace_wc_get_page_id( $page['parent'] ) : '' ); | |
} | |
return $return; | |
} | |
echo azweb_marketplace_create_woocommerce_default_page(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment