[Forminator Pro] find all forminator forms in WordPress multisite

have Forminator Pro in use with WordPress multisite. As the network admin, I'm interested in knowing the pages in the network that have a forminator form on them...when forms are getting published and changed. Any ideas of how to do that? Im sure i need to write a function or plugin for this. Just looking for some head starts. thanks

  • Konstantinos Xenos

    Hey jjt415 ,

    I've made a mu-plugin for you to have a solid start for what you're trying to achieve and you can always customize it further depending on the needs if you like.

    This mu-plugin will check all posts whenever they are "updated" to see if the [forminator_form] shortcode is used and add it to a custom option so you can have a list. As you can understand this list will only populate if there is a "post_updated" action so not all pages that have a form will be listed straight away. I did it this way because querying the whole database initially to get a list might've been very expensive/instensive to the server depending on how many blogs/posts this multisite has.

    How to install:
    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn't exist.
    2] Download the attached .zip and extract it. You will find a file named forminator-gather-post-forms.php
    3] Upload the file from the .zip into the mu-plugins directory.
    4] The final path should look like /wp-content/mu-plugins/forminator-gather-post-forms.php
    5] Everything should work after that automatically.

    You should now have a new Menu item called "Forminator Posts" in your Network Admin -> Dashboard area. When you click it it will show a page with all the information gathered about which Blog and Post has a Forminator Form like seen in the screenshot.

    Tell me if this worked for you and if you need further help to get a good start on this :slight_smile: .

    Regards,
    Konstantinos

    Plugin code preview:

    <?php
    /**
     * Plugin Name: Forminator - Gather all forms from Posts.
     * Plugin URI: https://premium.wpmudev.org/
     * Description: mu-plugin that adds an admin screen which shows all posts that contain the [forminator_form] shortcode.
     * Version: 1.0.0
     * Author: Konstantinos Xenos @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( ! class_exists( 'Forminator_Gather_Post_Forms' ) ) {
    
    	/**
    	 * Forminator_Gather_Post_Forms Class.
    	 */
    	class Forminator_Gather_Post_Forms {
    
    		/**
    		 * Constructor.
    		 */
    		public function __construct() {
    			add_action( 'init', array( $this, 'create_menus' ) );
    			add_action( 'post_updated', array( $this, 'check_posts_for_shortcode' ), 15, 3 );
    		}
    
    		/**
    		 * Creates the Super Admin Menus.
    		 */
    		public function create_menus() {
    			add_action( 'network_admin_menu', array( $this, 'populate_admin_menu' ) );
    		}
    
    		/**
    		 * Populate the admin menus.
    		 */
    		public static function populate_admin_menu() {
    			add_menu_page(
    				esc_html__( 'Forminator Posts', 'forminator-posts' ),
    				esc_html__( 'Forminator Posts', 'forminator-posts' ),
    				'manage_options',
    				'forminator-posts',
    				array( $this, 'create_page' ),
    				'dashicons-feedback'
    			);
    		}
    
    		/**
    		 * Checks if the option exists else creates it.
    		 */
    		public static function check_option() {
    			switch_to_blog( get_main_site_id() );
    
    			$option = get_option( 'my_network_forminator_pages' );
    
    			if ( ! $option ) {
    				$option = update_option( 'my_network_forminator_pages', array() );
    			}
    
    			restore_current_blog();
    		}
    
    		/**
    		 * Create the Super Admin page.
    		 */
    		public function create_page() {
    			self::check_option();
    			$option = get_option( 'my_network_forminator_pages' );
    			?>
    			<div class="wrap">
    				<h1><?php esc_attr_e( 'Forminator posts', 'forminator-posts' ); ?></h1>
    				<br class="clear" />
    				<table class="widefat">
    				<thead>
    				<tr>
    					<th><?php esc_attr_e( 'Blog', 'forminator-posts' ); ?></th>
    					<th><?php esc_attr_e( 'Post', 'forminator-posts' ); ?></th>
    					<th><?php esc_attr_e( 'Published', 'forminator-posts' ); ?></th>
    					<th><?php esc_attr_e( 'Updated', 'forminator-posts' ); ?></th>
    				</tr>
    				</thead>
    				<tbody>
    				<?php
    				if ( ! empty( $option ) ) {
    					foreach ( $option as $key => $value ) {
    						$info = explode( ',', $value );
    						switch_to_blog( $info[0] );
    						$_post = get_post( $info[1] );
    						?>
    						<tr>
    						<td><a href="<?php echo esc_url( get_admin_url( $info[0] ) ); ?>">#<?php echo esc_attr( $info[0] ); ?> - <?php echo esc_attr( get_bloginfo( 'name' ) ); ?></a></td>
    						<td><a href="<?php echo esc_url( get_admin_url( $info[0] ) . 'post.php?post=' . $info[1] . '&action=edit' ); ?>">#<?php echo esc_attr( $info[1] ); ?> - <?php echo esc_attr( $_post->post_title ); ?></a></td>
    						<td><?php echo esc_attr( $_post->post_date ); ?></td>
    						<td><?php echo esc_attr( $_post->post_modified ); ?></td>
    						</tr>
    						<?php
    						restore_current_blog();
    					}
    				} else {
    					?>
    					<tr>
    					<td colspan="3"><?php esc_attr_e( 'There are no posts to show.', 'forminator-posts' ); ?></td>
    					</tr>
    					<?php
    				}
    				?>
    				</tbody>
    				<tfoot>
    				<tr>
    					<th><?php esc_attr_e( 'Blog', 'forminator-posts' ); ?></th>
    					<th><?php esc_attr_e( 'Post', 'forminator-posts' ); ?></th>
    					<th><?php esc_attr_e( 'Published', 'forminator-posts' ); ?></th>
    					<th><?php esc_attr_e( 'Updated', 'forminator-posts' ); ?></th>
    				</tr>
    				</tfoot>
    			</table>
    			</div>
    			<?php
    		}
    
    		/**
    		 * Checks the post on update if a forminator shortcode is used.
    		 *
    		 * @param int   $post_id The post id.
    		 * @param array $post_after The post data after the save.
    		 * @param array $post_before The post data before the save.
    		 *
    		 * @return void
    		 */
    		public function check_posts_for_shortcode( $post_id, $post_after, $post_before ) {
    			self::check_option();
    			$blog_id = get_current_blog_id();
    
    			switch_to_blog( get_main_site_id() );
    			$option    = get_option( 'my_network_forminator_pages' );
    			$blog_post = $blog_id . ',' . $post_id;
    
    			if ( has_shortcode( $post_after->post_content, 'forminator_form' ) ) {
    				if ( ! in_array( $blog_post, $option, true ) ) {
    					$option[] = $blog_post;
    				}
    				update_option( 'my_network_forminator_pages', $option );
    			} else {
    				if ( ! empty( $option ) ) {
    					foreach ( $option as $key => $value ) {
    						if ( $blog_post === $value ) {
    							unset( $option[ $key ] );
    							update_option( 'my_network_forminator_pages', $option );
    						}
    					}
    				}
    			}
    
    			restore_current_blog();
    		}
    	}
    
    	new Forminator_Gather_Post_Forms();
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.