the Internet Windows Android

How to create your own registration page in Wordpress Multisite. Output of registration form

Create your own registration page for multisite instead of standard WP-Signup.php.

In the usual WordPress setup, the registration page (authorization, password reset) displays the WP-login.php file.

  • /wp-login.php - authorization
  • /wp-login.php?Action\u003dRegister - registration
  • /wp-login.php?Action\u003dLostPassword - Password Reset

For multisite in WP-Login.php there are separate conditions. So, when you click on the link /wp-login.php?Action\u003dRegister on multisaite, WordPress will make a redirect to the /wp-signup.php page. In many topics, the page looks not very attractive, so we will make our own.

Main Website Network

By default, WordPress opens the registration page (WP-Signup.php) on the main domain (website) network. However, you can make a separate registration page for each network site, even if they have different topics. We will consider the case when on all network sites have its own registration page, but the same topic is used and sites differ only in the language. If different topics are used, you will need to write more code.

functions.php?

Not. The name of this file seems to be mentioned in any article about WordPress. In our case, taking into account the fact that the registration functionality is designed for several sites, it makes sense to take it into Mu-plugins that are loaded when opening any site.

Lyrical digression

It is worth noting that MU-plugins are loaded earlier than ordinary plug-ins and until the WordPress kernel is fully downloaded, so the call for some functions can lead to fatabal errors in PHP. Similar "Early" download has its advantages. Let's say inside any theme can not be cling to some action that is triggered before downloading the Functions.php file from the topic. An example of this can serve as a JetPack_Module_loaded_Related-Posts plugin (Related-Posts - the name of the module) with which it is possible to track the activity of modules in JetPack. It is impossible to "clutch" from the topic file to this action, because the action has already worked before loading the topic - the plugins are loaded earlier. You can take a look at the common picture of the WordPress boot order on the Action Reference page in the Code.

Order in files

Mu-plugins may contain any number of files and any chart, which will seem logical to you. I adhere to about such a hierarchy:

| -Mu-plugins | - |load.php | - | - |Selena-Network | - | - | - | - -signup | - | - | - | - | --plugin.php | - | - | - | - | -... | - | - | - | -jetpack | - | - | - | - | - -plugin.php

In the Load.php file, all the necessary "plugins" are connected to our network:

LOAD TRASLATES FOR ALL ADDONS LOAD_MUPLUGIN_TEXTDOMAIN ("Selena_Network", "/ Selena-Network / Languages \u200b\u200b/"); // Network Signup Require WPMU_PLUGIN_DIR. "/selena-network/signup/plugin.php"; // Another Plugins // Require WPMU_PLUGIN_DIR ...

Inside the Selena-Network folder, plug-in folders are stored, each has its own plugin.php, which we connect on Load.php. It gives flexibility and the ability to quickly disable and include some things.

Address of registration page

To specify the address of the registration page, use the WP_SIGNUP_Location filter. It can be found inside the WP-Login.php file and it is he who is responsible for the redirect on the WP-Signup.php.

CASE "REGISTER": if (is_multisite ()) (WP_REDIRECT (Apply_Filters ("WP_SIGNUP_LOCATION", Network_site_URL ("WP-SignUp.php"))); EXIT;

Add your feature in MU-plugins / Selena-Network / Signup / plugin.php, which will give the address of the registration page on the current site:

Function selena_network_signup_page ($ URL) (RETURN HOME_URL (). "/ SignUp /";) Add_Filter ("WP_SIGNUP_Location", "selena_network_signup_page", 99);

selena_Network - Prefix, which I use in the names of all functions inside Mu-plugins on my site to avoid conflicts, should be replaced with its own unique prefix. The priority of adding filter 99, because some plugins, such as BBPress and Buddypress, can overwrite this address to your own (MU-plugins are loaded earlier than conventional plugins, see above). Please note that home_url () is used, instead of Network_site_URL (), to leave the visitor on the same domain. You can use any URL as an address.

Creating a page

Now let's create a page with site.com/signup/ through a regular interface, and in the daughter-subject folder template for our new page - page-signup.php. Instead of the word "Signup" you can use a unique ID.

Inside the new template, you must call the Selena_Network_signup_main () function, which will display the registration form.

It is worth noting that the whole process with templates is not required and instead you can create your own shortcode, which will also call the Selena_Network_signup_main () function.

wP-Signup.php and WP-ACTIVATE.PHP

Now we will create a function that will display the registration form. To do this, copy the files WP-Signup.php and WP-Activate.php from the root WordPress in MU-plugings / Selena-Network / Signup / (and do not forget to connect them inside MU-Plugins / Selena-Network / SignUp / plugin.php) . Further manipulations with files are extremely difficult and long to describe, so come to make them yourself. I will only describe what exactly needs to be done and publish the source files of your project:

  1. At the beginning of the file, delete all Require, Calling Functions and other code outside functions.
  2. Rename all the functions by adding unique prefixes to the names.
  3. The bottom part of the WP-Signup.php code is wrapped into the selena_network_signup_main function and in its very beginning to write Global $ Active_SignUp; .
  4. Replace layout on your own in the right places.

Inside WP-ACTIVATE.PHP you need to do about the same:

  1. Delete all code outside functions, wrap the layout into a separate function.
  2. Change the layout in places where it is necessary.

The WP-ACTIVATE.PHP file is responsible for the account activation page. As with the registration page, you need to create a separate template inside which to call a function from the WP-ACTIVATE.php file.

We send the activation letters

The registration page sends a letter to the visitor with reference to the activation of the account. By default, this is the WPMU_SIGNUP_USER_NOTIFICATION () function from the MS-FUNCTIONS.PHP file. Its functionality can be borrowed for its function. The reason why you want to refuse to use this function - it sends an account activation reference with WP-Activate.php. You can disable this feature using the WPMU_SIGNUP_USER_Notification filter by moving the false on it (if this is not to do, the activation letter will be sent twice, the okay, in fact two different letters).

Function Armyofselenagomez_wpmu_signup_user_notification ($ User, $ User_Mail, $ Key, $ Meta \u003d Array ()) (// ... // code from the WPMU_SIGNUP_USER_Notification () function WP_Mail ($ user_email, WP_SPECIALCHARS_DECODE ($ Subject), $ Message, $ Message_Headers) ; RETURN FALSE;) Add_Filter ("wpmu_signup_user_notification", "armyofselegenagomez_wpmu_signup_user_notification", 10, 4);

As a result, the registration page in the topic of Selena began to look much cleaner and careful.

Conclusion

The Internet has many other not very correct ways to make the same thing - Apache redirects, AJAX-forms that will not work without Java Script, etc. I didn't really like it very much, so I tried to do it most correctly on my own website.

I note that the edit files should be able to carefully and try not to move away from the source to the ranknesh, if WordPress changes the WP-SignUp.php and WP-ACTIVATE.php files, they were easier to compare them to find changes.

Do not forget to look at the source code of all the functions described above in order to fully deal with what and how it happens inside the code.

Bonus. Protection against spammers

Even the youngest sites on Wordpress are often subject to spam registration. You can write endless conditions for filtering bots, often more similar to the attempt to create an artificial intelligence 🙂 In the case of a multisite, a regular redirect in Apache helped me, with which when opening /wp-signup.php and /wp-acitvate.php, I asked to issue 404 (I am not an expert on setting up apache, so my rules may not be very correct).

RewriteEngine On RewriteBase / Rewriterule ^ WP-SignUp \\ .php - rewriterule ^ WP-Activate \\ .php - # Begin WordPress # Rules from WordPress by default Do not touch :) # ... # End Wordpress

P. S. I try to describe some third-party things as in detail as much as possible, because when I started, sometimes it was somehow to tell and explain many things. I also believe that similar small tips on other materials someone will be pushed to the study of something new and expanding their field of knowledge. Regular expressions are used in Reriterule records, they are not completely complex, for example, the symbol ^ means the beginning of the line.

Advertisements.

ln [-ffhinsv] original file [ target_File ] ln [-ffhinsv] original file ... target_Catalog LINK. source_File Target_File Program lN. Creates an entry in the directory (link) named target_File. On the target_File The same regimes will be installed on original file. References allow you to have several copies of one file or directory placed in different places, but without occupying disk space. There are two types of links, tight references and symbolic links. How reference points to original fileDepends on the type of link.

The LN command has the following options: -f if target_File Already exists, remove it so that you can create a link. This option cancels the option. -I.. -F if target_File Already exists and is a directory, remove it so that you can create a link. Option -F. Used with -f options or -I., in case none of them is specified, the option is meant. -f.. This option does not work without an option. -s.. -h if target_File or target_Catalog It is a symbolic link, not follow it. This option is useful in combination with the option. -f. To replace the symbolic reference, which indicates the directory. -i interactive mode. If a target_File There is, the user will be a request for removal in case of consent, lN. Remove target_File And create a new link. This option cancels the action option. -f.. -N analogue option -h, for compatibility with other implementations of the program lN.. -S Create a symbolic link. -V mode of display information about the progress of the program lN.. Default program lN. Creates a hard link. The strict link to the file, no different from the source file, and the changes made in the file are not dependent on behalf of which the appeal was made to it. Hard references cannot be references to directories, as well as can not be outside this file system. The symbolic link contains the name of the file to which it refers. When performing an operation open.(2) Over the symbolic reference uses the origal file. Call stat(2), made on a symbolic reference, will also return the source file. For information about the link you can use lstat.(2). To read the contents of the symbolic link, you can use the call readlink.(2). Unlike hard references, symbolic, can be on another file system and may indicate directories. With one or two arguments, program lN. creates a reference to the existing original file. The name for the link will be taken from the argument. target_File. If in the argument target_FileThe directory is not specified to create a reference, the current directory will be used if only the directory is specified, the link to the last element from original file. With more than two arguments, program lN. Creates links B. target_Catalog on all the indicated paths in original file. Links are obtained by the names of the source files. If program lN., caused in the form lINK.It is transmitted exactly two arguments, the transmitted arguments cannot be directories, in addition, in this form it does not accept any options. This is a simple form of use. Compatibility option -h, -I., -N. and -V.are intended for compatibility with other implementations of the program lN.And not recommended for use in scripts. Option -F. is an additional for

Allows you to use one WordPress setting for multiple sites at the same time. At the same time, each site receives its own tables in the database with a unique prefix.

Tables with data of registered users common to all network sites. This is a certain plus and registering one day you can access multiple sites. Moreover, on each site, the same account may have different rights. For example, on one site, the user can be the editor, and on another administrator.

In the usual WordPress installation, the registration page, authorization and password reset displays the WP-Login.php file.

  • wP-LOGIN.PHP - Authorization
  • wP-LOGIN.PHP? Action \u003d Register - Registration
  • wP-LOGIN.PHP? Action \u003d LostPassword - Password Reset

In the Multisite mode, the WordPress kernel starts to behave somewhat differently and when switching to the WP-Login.php link? Action \u003d Register will receive a redirect on WP-Signup.php. This is the registration page of your network, which is default in WordPress.

In addition to registering ordinary user accounts, you can create a new site on it if the superministrator has enabled such an opportunity in network settings (Network settings).

In most topics, the registration page does not look very good. Many themes are used by CSS frameworks, such as bootstrap, and their own specific classes for the stylization of different elements on the pages, so it is hard to write a single HTML that will be suitable for everyone.

But you should not despair if the page looks untidy. The WP-SignUp.php file is a great thing at first, when there is no time to work out each part of the site - you can focus on other more important pages and content.

When you are ready to make your own registration page, WP-Signup.php will be a good sample and an example, which is easy to understand the spectrum of functions that WordPress provides for processing and checking data entered by users and create new accounts.

Main Website Network

By default, WordPress opens the registration page (WP-Signup.php) on the main domain (website) network. Nevertheless, you can create registration pages for each network site, even if they have topics.

We will consider the case when one topic is used on all network sites, but there is a registration page on each of them. Sites differ in tongue (English and Russian), so the registration page will be displayed on the "native" language site. If the sites use different topics, everything will depend on which topics that will suit them the same layout (an excellent situation that can push you to unify all of its topics) or it is worth working out the pages individually.

Alternative to Functions.php.

Order in files

Mu-plugins may contain any number of files and a structure that will seem logical to you. I adhere to about such a hierarchy:

| Mu-plugins | | Load.php | | Selena-Network | | | Signup | | | | plugin.php | | | ... | | | jetpack | | | | plugin.php.

In the Load.php file, translations and all the necessary "plugins" are connected:

// Loading transfers for MU-plugins Load_muplugin_TextDomain ("Selena_Network", "/ Selena-Network / Languages \u200b\u200b/"); // Functional for Registration Page Require WPMU_PLUGIN_DIR. "/selena-network/signup/plugin.php"; // another plugin // Require WPMU_PLUGIN_DIR ...

Inside the Selena-Network directory, plug-in folders are stored. Each has its own plugin.php, which we connect on Load.php. This gives flexibility and the ability to instantly disable and include individual components on the working project in case of emergency.

Registration page

Having understood with where and how we will write code, you can go to the creation of the registration page.

Create a page with an example.org/signup/ through the usual interface. As an address, you can use any URL that will seem suitable for your project.

Redirect to the desired registration page

For WordPress to find out about our new registration page and made a redirect on it, when clicking on the "Register" link, the WP_SIGNUP_Location filter is used. It can be found inside the WP-Login.php and it is he who is responsible for the redirect on the default WP-Signup.php.

Case "Register": if (is_multisite ()) (WP_REDIRECT (Apply_Filters ("WP_SIGNUP_LOCATION", Network_site_URL ("WP-SignUp.php"))); exit; // ...

How do you remember, by default, the registration page opens on the main domain of the network. That is why Network_site_URL () is used here.

Add your filter handler to MU-plugins / Selena-Network / SignUp / plugin.php, which will be given the address of the registration page on the current site:

Function selena_network_signup_page ($ URL);) Add_Filter ("WP_SIGNUP_LOCATION", "Selena_Network_signup_page", 99);

selena_Network - Prefix, which I use in the names of all functions inside Mu-plugins on my site to avoid conflicts, should be replaced with its own unique prefix. The priority of adding filter 99, because some plugins, such as BBPress and Buddypress, can overwrite this address to your own (MU-plugins are loaded earlier than conventional plugins, see above).

Please note that home_url () is used, which, unlike Network_site_URL (), gives the address of the current site, and not the main network site.

WP-SIGNUP.php functionality

The WP-Signup.php file contains a large number of functions and code. To see the picture as a whole, you can use the folding code. As a rule, in English it is called "Code Folding".

At the very beginning of the file from 1 to 80, the line (in version 4.1.1), various checks are made and outputting the "start" page using Get_Header ().

Next, many methods are declared and before we start working with them, it is worth understanding what makes each function. Inside, many of these are often used other functions with the WPMU_ prefix, all of them are declared in the WP-Includes / MS-FUNCTIONS.php file. This section is hard to understand without seeing the code yourself. Below a small description of the basic functions in case you have difficulty.

  • wPMU_SIGNUP_STYLESHEET () - Output of additional CSS on the registration page.
  • show_blog_form () - fields for site registration (address, name visibility for search engines).
  • validate_blog_form () - checking the entered site address and names using WPMU_VALIDATE_BLOG_SIGNUP ().
  • show_user_form () - fields for registering the user (login and email address).
  • validate_user_form () - checking the login entered and addresses. Mail with WPMU_VALIDATE_USER_SIGNUP ().
  • signup_another_blog () - fields for registering new sites using SHOW_BLOG_FORM () for users who are already registered on the site.
  • validate_another_blog_signup () - checks the site address and name using Validate_blog_form ().
  • signUp_user () is the main function to output the field of registration page.
  • validate_user_signup () - checks the login and email address. Mail with Validate_user_Form ().
  • signUp_blog () - fields for entering the address, name and visibility of the site (second registration step) using show_blog_form ().
  • validate_blog_signup () - checks the login, email address. Mail, address and site name.

At the very bottom of the WP-SignUp.php file (from 646 stitching in version 4.1.1), the main logic of the registration page, which uses the above methods. This part of the code is not submitted to the function. At the end, get_footer () is called.

Copy the functional WP-Signup.php

Next, the procedure for copying a WP-SignUp.php in MU-plug-ins will be described and making changes to Fork. It may seem not the right way. Instead, you can write your functions from scratch to check and output forms using classes, not conventional functions. In my opinion, the WP-Signup.php already has all the necessary logic for our page, it remains only to make minor changes.

When updating WordPress, the WP-SignUp.php changes from time to time, but this does not mean that with each release will have to synchronize its "forc". Functions inside WP-SignUp.php are essentially engaged only by the HTML output, data check, creating accounts and sites are engaged in the WPMU_ prefix, declared in MS-FUNCTIONS.PHP.

We will create a function that will display the registration form on the page. To do this, copy WP-Signup.php from WordPress root in Mu-plugings / Selena-Network / SignUp /. Connect it inside MU-PUGINS / Selena-Network / Signup / plugin.php).

Require WPMU_PLUGIN_DIR. "/selena-network/signup/wp-signup.php";

Delete all Require and unnecessary checks from the very beginning of the copied file. In version 4.1.1, this is the entire code from 1 to 80 lines.

Now we are ready to create the main function to display the registration form. To do this, all logic from line 646 and to the very end of the file we will be transferred to the function called selena_network_signup_main. At the very end, you delete two unnecessary closing

(lines 722 and 723), as well as the Call GET_FOOTER ().

In the newly created selena_network_signup_main () at the very beginning, we will declare the global variable Active_SignUp that all other methods from this file use. And add the Before_Signup_form event call, which we deleted from the start of the file.

Function Selena_Network_signup_main () (Global $ Active_SignUp; do_action ("before_signup_form"); // ...)

Now it remains only to change the layout in all places where it is necessary and the registration page is ready.

Output of registration form

There are at least two options here. A more convenient way to create a shortcode and place it on the page through the usual editor.

// Create a Network_signup Add_Shortcode shortcode ("Network_signup", "Selena_Network_signup_main");

The second option is to create a Page-SignUp.php page in the daughter theme folder. Instead of the word "Signup" you can use a unique ID assigned to the page. Inside the template, add the required layout and make a select selena_network_signup_main () in the right place.

As a result, my registration page began to look much better and cleaner.

Activation page

By default, WordPress conventionally divides the registration process in Multisite for two steps - filling out the form on the site and activation of the account when you go on the link sent in an email. After you fill out the form created in the previous section, WordPress sends a letter with a small instruction and reference to activate the account.

For the output of the activation page, the WP-Activate.php file is answered located in the WordPress root directory. WP-ACTIVATE.PHP can also be completely changed. The process is similar to what we have already done for WP-SignUp.php.

Create the EXAMPLE.ORG/ACTIVATE / page through the normal interface. As an address, use any URL that will seem suitable for you.

Copy the WP-Activate.php file to your Mu-plugins and connect it to MU-PUGINS / Selena-Network / Signup / plugin.php.

Require WPMU_PLUGIN_DIR. "/selena-network/signup/wp-activate.php";

Inside, not so many contents, unlike WP-SignUp.php. The file performs a single operation - activates the account if the correct key is obtained and displays an error message or successful operation.

Delete all unnecessary checks and Require - from 1 to 69 line in Wordpress 4.1.1. At the very end, remove the GET_FOOTER () call. The remaining contents by transferring to the selena_network_activate_main () function.

It is interesting to notice that the WP_INSTALLING constant was announced here before loading WordPress (WP-Load.php). Its presence causes WordPress not to load plugins.

As in the case of the registration page, it remains only to fix the layout where it is necessary. You can also change the text of the displayed messages (in this case, do not forget to add a text domain of your Mu-plugins to all function-translators, it is not installed anywhere else).

The finished function can be used on a predetermined page through a shortcode or a separate template in a subsidiary.

Activation letters with proper references

The activation page is ready to work, but WordPress does not know about it and will still send activation letters with reference to WP-ACTIVATE.PHP. Unlike WP-SignUp.php there is no filter, which would allow to change the address. Instead, you need to write your feature that will send letters with correct links.

At the time of filling and sending a form on the registration page WordPress calls WPMU_SIGNUP_ user.() or wpmu_signup_ blog.() Depending on the type of registration. Both functions create a new entry in the WP_SIGNUPS table filling it with the necessary content, among which there is an account activation key.

After, depending on the function, WPMU_SIGNUP_ is called user._Notification () or WPMU_SIGNUP_ blog._Notification (). Both functions have similar functionality - generate and send a letter with an activation reference, but take different arguments. In both there are filters for "interception" of events.

If (! Apply_filters ("wpmu_signup_user_notification", $ user, $ user_email, $ key, $ META)) Return False;

To activate accounts with blog creation:

If (! Apply_filters ("wpmu_signup_blog_notification", $ domain, $ path, $ title, $ user, $ user_email, $ key, $ META)) (Return False;)

It remains only to write your handlers within which you send letters via WP_mail (), and at the very end necessarily to give false so that WordPress does not send an activation letter twice - one of yours, another - letter by default with a link to WP-Activate.php.

Function selena_network_wpmu_signup_user_notification ($ user, $ user_email, $ key, $ Meta \u003d Array ()) (// Generate a header, text and letter headers // ... // Send a letter or add a CRON-task to send a letter WP_Mail ($ User_Email , WP_SPECIALCHARS_DECODE ($ subject), $ message, $ message_headers); // Recover False so that WordPress does not send an activation letter twice Return False;) Add_Filter ("WPMU_SIGNUP_UVER_NOTIFICATION", "selena_network_wpmu_signup_user_notification", 10, 4);

If you send letters through the SMTP server or the number of registrations is very large, you should think about not sending letters instantly. Instead, you can add CRON-tasks using WordPress CRON.

Close access to WP-Signup.php and WP-ACTIVATE.PHP

Having created its own registration and activation pages, it may be necessary to close the "originals". For example, if there are additional fields on the registration page, which must be fill in. Also, many WordPress sites are exposed to spam registrations.

Solve two problems with one action can be asked Apache to give 404 in the case of attempting to open these pages. To do this, you just need to register a pair of additional rewriterule to your configuration file or.htaccess.

RewriteEngine On RewriteBase / # Knowledge of regular expressions will never be superfluous :) Rewriterule ^ WP-SignUp \\ .php - rewriterule ^ WP-Activate \\ .php - # Begin WordPress # Rules from WordPress by default Do not touch :) # ... # End Wordpress.

Conclusion

For this and many other "problems" associated with WordPress on the Internet there are many solutions. For example, to create registration and activation pages, some are offered to rewrite the original WP-SignUp.php and WP-ACTIVATE.PHP. This should not be done because in the case of the WordPress update, you will lose all changes made to files, and you can not, check the integrity of the kernel using.

When developing any addition, topics or solutions should be spent some time to deal with what is happening inside WordPress. There are many useful debug tools for this.

P.S.

To automatically assign different roles, new users can use MultiSite User Management plugin.

If you have any questions or difficulties during the creation of registration and activation pages after reading the article, leave a comment and we will answer.

27.03.2015 27.03.2015

WordPress developer. He loves order in everything and deal in new tools. Inspired by the architecture of the Symfony components.