Create posts, pages and menus in Wordpress MU - wordpress

I'm using Wordpress (with multisite capabilities enable) to create my network.
When I add a new site, it's needed to create some posts, pages and menus into this new site.
I need to create a default form (with WP Contact Form 7 plugin) too.
While I'm looking for this answer, I'm doing this manually: add the new site, create posts, pages, menus and default contact form, then I give the user and password to new user.
Thanks!!

It can be done with a Must Use Plugin and the action hook wpmu_new_blog.
<?php
/**
* Plugin Name: Create custom content on site creation
*/
add_action( 'wpmu_new_blog', 'custom_items_so_23276440', 10, 6 );
function custom_items_so_23276440( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$default_page = array(
'post_title' => 'Default title',
'post_content' => '<h2>Default content</h2>',
'post_status' => 'publish',
'post_type' => 'page'
);
// insert the post into the database
wp_insert_post( $default_page );
restore_current_blog();
}
To create the menus, use the functions wp_create_nav_menu and wp_update_nav_menu_item. I'm not sure about the contact form, you'll have to inspect the database to see how CF7 does it, maybe it has some handy function to create a form programatically...
There's another technique using the file wp-content/install.php explained in How to Create a Custom WordPress Install Package?

Related

Custom page created by a wordpress plugin

I am trying to figure out how to create a Wordpress plugin that adds a page to the Wordpress website on which it is installed.
I came up with the following which works and adds the page.
However, it's far from what I am trying to achieve:
How to change the entire page? Not just the content? Right now, it has all the header and footer and navbar.
How to have PHP code in the page, not just static content?
Is it possible to have everything under a url (https://some-url.com/my-plugin/) routed to this same page?
For example:
https://some-url.com/my-plugin/ -> run my page
https://some-url.com/my-plugin/foo/ -> run my page
https://some-url.com/my-plugin/foo2/abc/ -> run my page
etc.
<?php
/**
* Plugin Name: MyPlugin
* Plugin URI: myplugin.com
* Description: MyPlugin
* Version: 1.0
* Author: Mike
* Author URI: myplugin.com
*/
define( 'MYPLUGIN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
function create_page() {
$post_data = array(
'post_title' => 'Test of my plugin',
# How to change the entire page? Not just the content?
# Also, how to have PHP code in the page, not just static content?
'post_content' => 'Place all your body content for the post in this line.',
'post_status' => 'publish', // Automatically publish the post.
'post_type' => 'page', // defaults to "post".
'post_name' => 'my-plugin', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugin-page?/
);
// Lets insert the page now.
wp_insert_post( $post_data );
}
function plugin_activation() {
create__page();
}
function plugin_deactivation() {
}
register_activation_hook( __FILE__, 'plugin_activation' );
register_deactivation_hook( __FILE__, 'plugin_deactivation' );
You want wp_insert_post()
Usage:
$postarr = [
'post_title' => 'My Page Title', // <title> tag
'post_content' => '<p>page content</p>', // html for page content
'post_type' => 'page', // blog post is default
'post_name' => 'my-plugion-page', // url slug (will 'slugify' post_title if empty) https://some-url.com/my-plugion-page?/
];
$post_id = wp_insert_post($postarr);
For example, in your $postarr if you want it to be a page (not a blog post), use 'post_type' => 'page'. For the page title 'post_title' => 'My New Page' and so on.
See: https://developer.wordpress.org/reference/functions/wp_insert_post/
Updated for additions to user question
If you don't want it to follow the theme templates, then you'll need to hook into the 'page_template' filter and use a template file you create in your plugin. See: https://wordpress.stackexchange.com/questions/3396/create-custom-page-templates-with-plugins
Depends on the situation, you can add code in your custom template, use shortcodes or custom Gutenberg blocks. You can use Advanced Custom Fields on in the Admin UI and use get_field() in your code, etc. You have lots of options in WordPress. Once you have a custom template you can just add regular PHP in there.
Yes, but you'll need to use regex and create rewrite rules using add_rewrite_rule(). Here are links that help with creating rewrite rules in WordPress
add_rewrite_rule
add_rewrite_tag
flush_rewrite_rules
WP_Rewrite API

Placing Wordpress plugin name at the top of the amdin header

I have a Wordpress plugin which I've named 'Solve Maths'. I want this plugin name to be displayed only at the Admin header of the Arthur alone for easy access.
I used the admin_head action hook but once I installed the plugin, the system tells me my plugin has generated these number of characters at the header.
<?php function Solve_Maths{ echo "<a href='Solve_Maths.php'>Solve Maths</a>";} add_action('admin_head','Solve_Maths');?>
The Solve_Maths.php is the name of the main plugin file with the header information. I want this file name in the tag to be shown at the admin header of the user and should execute the file when the link is linked. Thank you all for your help.
This is not the correct way to do it. Please check the WordPress Codex and refer to the function add_node: https://codex.wordpress.org/Function_Reference/add_node
add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );
function toolbar_link_to_mypage( $wp_admin_bar ) {
$args = array(
'id' => 'my_page',
'title' => 'My Page',
'href' => 'http://example.com/my-page/',
'meta' => array( 'class' => 'my-toolbar-page' )
);
$wp_admin_bar->add_node( $args );
}

Add a user along with one custom post type

I am developing this plugin that admin can add a user in the backend and when user is created, plugin can automatically generate one custom post which I have added to the theme. The custom post will store user ID that is just created (or if it is possible make that user an author of the post)
I wonder if what I have mentioned above is possible practically. If anybody has any better advice, I am open for any suggestions.
Thank you in advance
I'm not sure that a unique custom post type per user is the best way to implement what you're wanting to achieve. If you have 100 users, you will have 100 custom post types making the wp-admin a nightmare as the left menu would grow with so many menu-items.
Wouldn't it be easier to just use a normal post type and then have the page that shows the user's dashboard filter the posts to only show posts where the user is the post_author? You could then add a hook to catch when a user registers and create the example post, you could modify the code below and add it to your functions.php:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$userPostsCategory = 3;
// Create post object
$my_post = array(
'post_title' => 'Sample Story' ),
'post_content' => 'You can edit this or create a new story',
'post_status' => 'publish',
'post_author' => user_id,
'post_category' => array( $userPostsCategory )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
This method will lower the number of customisations you'd have to do to your themes and make management of the posts a little easier.
Further reading on this:
User registration hook
Inserting a post using wp_insert_post

Creating pages from Ninja form data

I've created a WordPress page with a Ninja form on it that collects miscellaneous data about a product, including some uploaded images. The page with the form is accessible from the main menu by clicking the "Input" item, so the user doesn't need to access the backend to upload their product data.
I now want to put this data into a custom post type called "Listing." There will eventually be thousands of these data sets and so thousands of "Listing" pages, as people come to the site, click Input in the main menu to get to the page with the Ninja form and fill it out.
Could someone tell me how they would go about now building these listing pages from the data the form has collected?
I'm running Ninja's Front-End Post option which supposedly will create a page from the form data. This plugin has some Post creation settings where you can select the post type to create, but this isn't working for me. I would expect the submitted form data to show up under dashboard | Listings, but there's nothing there after submitting the form.
Has anyone gotten this to work?
Thanks for your help.
I think you can use only Ninja Forms without extensions, and hook directly in 'ninja_forms_after_submission' that fires after submission and allow you to use data submitted and perform actions.
This is a starter codebase to achieve your result, but needs to be customized on your needs and your form structure.
add_action( 'ninja_forms_after_submission', 'create_page_from_ninjaform' );
function create_page_from_ninjaform( $form_data ){
// your fields data
$form_fields = $form_data[ 'fields' ];
// !!! this is an example, it depends form fields in your form
$title = $form_fields[ 1 ][ 'value' ];
$content = $form_fields[ 2 ][ 'value' ];
$sample_meta_field = $form_fields[ 3 ][ 'value' ];
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'listing', // be sure this is the post type name
);
$new_post_id = wp_insert_post( $new_post );
update_post_meta( $new_post_id, 'your_meta_key', $sample_meta_field );
}
This code should be copied in functions.php file
Not tested of course.
Good luck ;)
The Ninja Forms Front-end Posting extension isn't really meant for displaying form submission data on the front end.
From: https://ninjaforms.com/extensions/front-end-posting/
"The Ninja Forms Front-end Posting extension gives you the power of the WordPress post editor on any publicly viewable page you choose."
If you want to show Ninja Forms submission data on the front end, you will have to retrieve them from the database with code in functions.php or by writing a plugin (recommended). You could then parse and manipulate them and create a shortcode that would allow you to insert your formatted submission data easily in Wordpress posts or pages.
Here's a link to a feature request, asking for the same thing. The author of that request posted a link to a plugin (click Download as Plugin) they wrote which may do what you want or give you further insights as to how you could implement this.
https://github.com/wpninjas/ninja-forms/issues/892
If you do not mind paying a little money for a plugin I would recommend using gravity forms rather then ninja forms for more advanced stuff like this.
I manually create a custom post type "oproep" and used a gravityforms plugin to create a custom post from type oproep when an user submits the form.
Because you use custom post type archive pages www.mysite.com/oproep will be automatically created so you already have a list of "Listings". The singe pages www.mysite.com/oproep/title will also be created for you by default, you could override these templates as well if you would like depending on your theme.
The only thing you have to do is add a few php lines to your functions.php (or write your own plugin) that adds the custom post type. The rest all works automatically.
I went so far as writing code to make users able to edit their submissions, read custom taxonomy tags in dropdowns etc. You got lots and lots of more options using gravity forms.
FrancescoCarlucci's answer is correct, but just adding an additional comment: in case you want to specify by form field IDs which fields should go where in your post, NinjaForms passes the ID as a number (in my case for example, I needed field 136 for my post title). It may have been obvious but I racked my brain for a while until I figured it out.
function create_post($form_data) {
$form_fields = $form_data[ 'fields' ];
$post_fields = array(
'post_content' => '',
'post_content_filtered' => '',
'post_title' => '',
'post_excerpt' => '',
'post_status' => 'pending',
'post_type' => 'post',
);
foreach ($form_fields as $field) {
$field_id = $field[ 'id' ];
$field_key = $field[ 'key' ];
$field_value = $field[ 'value' ];
if ($field_id == 136) {
$post_fields['post_title'] = $field_value;
}
}
wp_insert_post($post_fields, true);
}

Wordpress Profile File / Page

I would like to create a file in the Wordpress theme, where i will add my own code, edit profile, show profile information, and perhaps an ability to insert posts / meta data programmatically.
So it needs to be www.mysite.com/profile.php or www.mysite.com/profile/
I do not want to use Buddy Press or any other plugin.
I know how the template system works, i do not want a page template.
It will probably be a class, later on, i do not want to change .htaccess file, and if i must i would appreciated filter function how to do this from functions.php
Basically just a simple .php file i can link to, located in theme root.
include('../../../wp-load.php');
and write any code i would like to.
Any creative solution that is not too "hacky" would be appreciated.
Spent around 2 days googling bashing my head on this, before i decided to ask question.
Thank you very much.
Ok, I managed to do this, took me 2 days to figure it out. Here is how I managed to do it:
Make a plugin folder.
In that plugin folder make 1x php file. so index.php
Ok so first thing we need to register plugin I did it like this, in your index.php paste
this code.
function activate_profile_plugin() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'activate_profile_plugin' );
Then we need a function when you register a plugin only once register profile pages.
function create_profile_page( $title, $slug, $post_type, $shortcode, $template = null ) {
//Check if the page with this name exists.
if(!get_page_by_title($title)) {
// if not :
$page_id = -1;
$page_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_content' => $shortcode,
'post_author' => 1, // Administrator is creating the page
'post_title' => $title,
'post_name' => strtolower( $slug ),
'post_status' => 'publish',
'post_type' => strtolower( $post_type )
)
);
// If a template is specified in the function arguments, let's apply it
if( null != $template ) {
update_post_meta( get_the_ID(), '_wp_page_template', $template );
} // end if
return $page_id;
}
}
Ok so we created function which programatically register pages. It has 5 paramethers.
is Title
Slug
Post type
Shortcode.
Template
For the shortcode template you need to make a shortcode with the complete page output
and add it as a parameter to this function, so for registration page it will be a shortcode with the registration forms etc.
For example :
function registration_shortcode(){
echo 'Wellcome to Registration page';
}
add_shortcode('registration_output', 'registration_shortcode');
Next thing we need to call it once only when plugin loads.
so we do this :
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
create_profile_page('Registration', 'registration', 'page', '[registration_output]');
create_profile_page('Profile', 'profile', 'page', '[profile_shortcode]');
create_profile_page('Profil Edit', 'profile-edit', 'page', '[edit_shortcode]');
}
}
add_action( 'admin_init', 'load_plugin' );
Ok so this will execute only once when plugin loads and it will create 3 Pages, which are Profile, Registration and Profile Edit.
And that's it, you have your front-end user profile blank pages, and you can write page output in shortcodes ,create more pages, put any forms or elements you like and create decent profile (which doesn't have any stuff you don't need in it like plugins. )
Hope this helps, it was painful for me to figure this out. Cheers!

Resources