On plugin activation, create page and set template? - php

I'm creating a plugin that I want to create a page when it is activated and also set the template that it's using.
I've done the first part in that on activation it creates the page, how can I set the template? This is what I've attempted but it just picks the default template:
if ( $theme_file = locate_template( array( 'contact.php' ) ) ) {
$template = $theme_file;
} else {
$template = plugin_dir_path( __FILE__ ) . 'templates/contact.php';
}
//post status and options
$post = array(
'comment_status' => 'closed',
'ping_status' => 'closed' ,
'post_author' => 1,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'Contact',
'post_status' => 'publish' ,
'post_title' => 'Contact',
'post_type' => 'page',
'page_template' => $template
);
wp_insert_post( $post );

add_filter( 'page_template', 'wp_page_template' );
function wp_page_template( $page_template )
{
if ( is_page( 'Contact' ) ) {
$page_template = plugin_dir_path( __FILE__ ) . 'templates/contact.php';
}
return $page_template;
}
try this...

Related

How do I add page to wordpress website and giving it a specific short code via custom plugin

I have made an action that allows a page to be made if the switch in the setting is checked. After it is confirmed it's checked it does create the page, but without the template added. How do I get my template to work?
add_action( 'admin_init', 'cart_page' );
function cart_page() {
//Add cart page to website
if ( get_option( 'cart' ) === "checked" AND get_option('cart_exist') === false) {
// IF CART HAS BEEN CHECKED
$new_page_id = wp_insert_post( array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
// Assign page template
'page_template' => plugins_url('page_templates/cart_template.php', __FILE__ )
) );
wp_insert_post($new_page_id);
update_option( 'cart_exist', true );
}
else if (get_option('cart') === "" AND get_option('cart_exist') === true) {
// IF CUSTOMER DOES NOT WANT CART
update_option( 'cart_exist', false );
}
}
This is what my template page looks like in the plugin.
get_header();
echo do_shortcode( '[cart_portal]' );
get_footer();
?>
In my case, I have figured out a solution for the simple problem. If I just need to add a shortcode I can add it to 'post_content'. I would still like to know how to add template. But you can use a file to import the entire layout if you'd like still. But it cannot be used with visual composers.
See example below...
$new_page_id = array(
'post_title' => 'Cart',
'post_type' => 'page',
'post_name' => 'Cart',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_content' => '[cart_portal]',
'post_status' => 'publish',
'post_author' => get_user_by( 'id', 1 )->user_id,
'menu_order' => 0,
);
wp_insert_post($new_page_id);

wp_insert_user wont return the ID of the user

I have a sort of a marketplace website where users add a product then register. The user gets added and product gets added but I cant get the user ID to attach to the product after using wp_insert_user. Here is what I have.
in my page-list.php
if(isset($_POST['user_signup']) == '1') {
$user_login = $_POST['email_seller'];
$pas1 = $_POST['pass_seller'];
$user_email = $_POST['email_seller'];
$seller_name = $_POST['name_seller'];
$userdata = array(
'user_login' => $user_login,
'user_email' => $user_email,
'user_pass' => $pas1,
'role' => 'seller',
'first_name' => $user_email,
'last_name' => $user_email,
);
$user_id = wp_insert_user( $userdata ) ;
and the code I have in my functions.php
add_action( 'user_register', 'create_new_user_posts', 10, 1 );
function create_new_user_posts($user_id){
if (!$user_id>0)
return;
$post_title = $_POST['post_title'];
$post_cat = $_POST['cat'];
$term_id = get_term( $post_cat, 'product_cat' );
$post_category = $term_id->slug;
$post_cat_two = $_POST['cat_two'];
$new_post = array(
'ID' => '',
'post_author' => $user_id,
'post_content' => $post_content,
'post_title' => wp_strip_all_tags($post_title),
'post_status' => 'publish',
'post_type' => 'product',
'post_excerpt'=> $post_excerpt
);
$post_id = wp_insert_post($new_post);
wp_set_object_terms( $post_id, $user_id, 'soldby', false );
}

Set saved attachment as featured image in Wordpress

In Wordpress i have a function that correctly save a new processed image as attachment.
What i need is to set as featured image the attachment after its saving.
Here the function:
/* Save attachment */
$siteurl = get_option( 'siteurl' );
$file_info = getimagesize( $newFileName );
//create an array of attachment data to insert into wp_posts table
$tableParams = array(
'post_author' => get_current_user_id(),
'post_date' => current_time( 'mysql' ),
'post_date_gmt' => current_time( 'mysql' ),
'post_title' => $_POST['title'] . '-' . $_POST['filter'],
'post_status' => 'inherit',
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_name' => sanitize_title_with_dashes( str_replace( "_", "-", $_POST['name'] . '-' . $_POST['filter'] ) ),
'post_modified' => current_time( 'mysql' ),
'post_modified_gmt' => current_time( 'mysql' ),
'post_parent' => 0,
'post_type' => 'attachment',
'guid' => $newFileName,
'post_mime_type' => $file_info['mime'],
'post_excerpt' => '',
'post_content' => ''
);
// insert the database record
$attachmentID = wp_insert_attachment( $tableParams, $newFileName, 0 );
// generate metadata and thumbnails
if ( $attachmentData = wp_generate_attachment_metadata( $attachmentID, $newFileName ) ) {
wp_update_attachment_metadata( $attachmentID, $attachmentData );
}
And i call it in javascript:
ajaxcall = jQuery.post(ajaxparam.ajax_url,
{
action: 'new_image',
image: imageData,
format: format,
title: imageTitle,
name: imageName,
filename: largeImage,
filter: effect,
nonce: ajaxparam.nonce,
});
jQuery.when(ajaxcall).done(function(){
editor.insertContent(..............................);
});
So i tried to add to the function:
[......]
if ( $attachmentData = wp_generate_attachment_metadata( $attachmentID, $newFileName ) ) {
wp_update_attachment_metadata( $attachmentID, $attachmentData );
}
global $post;
set_post_thumbnail($post->ID, $attachmentID);
or
add_post_meta($post->ID, '_thumbnail_id', $attachmentID);
or
update_post_meta( $post->ID, '_thumbnail_id', $attachmentID );
in any case without success.
Thanks for any suggestion.

Wordpress, add post to custom taxonomy's category

I am trying to add a post to a category under taxonomy cate. The code I am using is:
$user = get_user_by( 'email', $_POST['user'] );
$id = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user->ID,
'taxonomy' => ('cate'),
'post_type' => 'ad',
'post_category' => array(425),
'post_status' => 'publish',
);
$user_id = wp_insert_post($id);
if ( ! is_wp_error( $user_id ) ) {
$odgovor["success"] = 1;
}
The post is added but it's added under category 'uncategorized" and not under desired category ID. This system works properly when custom post type is not used. (In this case taxonomy 'cate')
Any ideas?
You need wp_set_object_terms, which takes post ID, terms, taxonomy, and append as parameters. For example:
$user_id = wp_insert_post( $id );
wp_set_object_terms( $user_id, 'cate', 'category', true );
I solved it like this:
$id = array(
'post_title' => $_POST['title'],
'post_content' => $_POST['content'],
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user->ID,
'post_type' => 'ad',
'post_status' => 'publish',
);
$user_id = wp_insert_post($id);
wp_set_object_terms($user_id, 416, 'cate', true);
if ( ! is_wp_error( $user_id ) ) {
$odgovor["success"] = 1;
}
Josh showed me the way, but his syntax wasn't right. It' category first, taxonomy second, and some things had to be removed.

Adding items to menu not working in multisite

I have a custom code that it will automatically add the category to the menu in multisite. Lets say I have a category name "Cat1" then once I run my code this will automatically added to the menu. However 1 issue is that it will added if the blog is loggedin. I want to be added if it is not logging in.
Here is my code:
require( dirname(__FILE__) . '/wp-load.php' );
require_once( dirname(__FILE__) . '/wp-includes/ms-functions.php');
require_once( dirname(__FILE__) . '/wp-includes/user.php');
require_once( dirname(__FILE__) . '/wp-admin/includes/taxonomy.php');
require_once( dirname(__FILE__) . '/wp-admin/includes/nav-menu.php' );
$_nav_menu_selected_id = wp_update_nav_menu_object( 0, array('menu-name' => "Primary Menu") );
$test = array(
array(
'menu-item-db-id' => 0,
'menu-item-object-id' => 2,
'menu-item-object' => 'category',
'menu-item-parent-id' => 0,
'menu-item-type' => 'taxonomy',
'menu-item-title' => 'Rome',
'menu-item-url' => 'http://sub.example.com/love/',
'menu-item-target' => '',
'menu-item-classes' => '',
'menu-item-xfn' => '',
'menu-item-description' => ''
)
);
if($item_ids = wp_save_nav_menu_items( $_nav_menu_selected_id, $test )) {
foreach( $item_ids AS $single_item_id ) {
$my_post = array (
'ID' => $single_item_id,
'post_status' => 'publish',
'post_name' => $single_item_id
);
wp_update_post($my_post);
}
}
$menu_locations = array(
'main-menu' => $_nav_menu_selected_id,
'footer-menu' => $_nav_menu_selected_id
);
set_theme_mod( 'nav_menu_locations', $menu_locations );
If you run above example blog1.mysite.com it will added if I'm logging in on that blog. But if not they it will not added. However if I run it on www.mysite.com (not a subdomain) even if I'm not logging in it will added the category to the menu.

Categories