Custom field for client sites

I’ve created a small plugin to add a custom field (webhost) to a child site. You can see it on Github:

The field shows up, but if I enter a value while connecting to a new site, it’s not saved. When I edit the site later it does get saved.

How can i change the code so it will work for new sites as well?

I’ve tried to add this, but it didn’t work:

//  Update the Webhost field in the database of the added new site
add_action( 'mainwp_added_new_site', 'jkws_mainwp_update_site', 10, 1 );
2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Hey @josklever

Please try this code: add_action( 'mainwp_manage_sites_edit', 'jkws_mainwp_manage_sites_edit', 10, 1 ) - Pastebin.com

Due to the content of the code, Discourse is refusing to paste it here.

It seems to work!

Apparently I had the wrong hook. I found mine on mainwp_added_new_site
Did that change? In that case it should be updated.

It looks like the documentation hasn’t been corrected yet?

Hi @josklever

I will check with the development team if there were any changes to this hook and update the dev site if necessary.

Will update you here once I have more info.

1 Like

I’m happy to hear the code worked.

That hook is still in the code, so it should be working.
If you wish, you can send us the code where you used it and the team can take a look.

Hi @bojan, I’m not sure about your last comment, because it seemed to be delayed in the discussion. I used the code in my original attempt, but it didn’t work. The devs came back with another hook, that does work, but is not in the documentation. So it looks like it was renamed and the docs need to be adjusted.

Sorry about the confusion.

The mainwp_added_new_site hook is still in the code and the dev team believes it should be working. So if you wish, you can share your old code with us and we can take a look why it didn’t work.

I shared that code in my original post. And the complete code is available there as well (on Github).

The hook that the devs shared can’t be found in the documentation, so even if the old hook is still available, the new one is not documented yet. And I’m not sure why they both are available.

We’ve created an entry for the new hook: mainwp_site_added

The team cannot identify why the old hook didn’t work without the full code, which they are unable to find on GitHub.

1 Like

As I posted in my original message, it was just the same function that is called for editing the site, but also hooking it to the hook I mentioned. All I had to change was the name of the hook.

2 Likes

This is how the team just used the hook to test it out:

add_action( 'mainwp_added_new_site', 'jkws_save_webhost', 10, 1 );
function jkws_save_webhost( $site_id ) {
    $webhost = isset( $_POST['mainwp_managesites_edit_webhost'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_managesites_edit_webhost'] ) ) : '';
    apply_filters( 'mainwp_updatewebsiteoptions', false, $site_id, 'webhost', $webhost );
}

They have verified that the value 99999999999999999999 was added to the site option:

The full plugin code that didn’t work correctly is: <?php/** * Plugin Name: JKWS Webhost Information for MainWP * Description: - Pastebin.com

Hi @josklever,
Thanks for sharing the code. I also tried on my setup and confirmed that the value is not saved when the mainwp_added_new_site action is used.

To me, the code looks correct, so I passed this to our devs to check again and make sure that hook is set and documented properly.

When I have new info, I will post it.

On the other hand, it’s good to see that your custom snippet works correctly with the mainwp_site_added action.

Thanks

2 Likes

Hi @bogdan,

It’s a small project to learn more about creating a plugin like this and to learn more about GitHub, but I have some more steps to go. As soon as I have some more time, I’ll add some issues to this plugin for additional features and create pull requests etc.
Ideas and contributions are always welcome! :slight_smile:

1 Like

Hey Jos,

It is very nice to see interest in custom development for MainWP. Small projects become big very fast :smile:

I looked into the original problem more, and found that the mainwp_added_new_site action hook works properly after all, but not 100% sure why it failed in first attempts.

today, we tried with:

add_action( 'mainwp_update_site', 'jkws_save_webhost', 10, 1 );
function jkws_save_webhost( $site_id ) {
	$webhost = isset( $_POST['mainwp_managesites_edit_webhost'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_managesites_edit_webhost'] ) ) : '';
	apply_filters( 'mainwp_updatewebsiteoptions', false, $site_id, 'webhost', $webhost );
}


add_action( 'mainwp_added_new_site', 'jkws_mainwp_added_new_site_save_webhost', 10, 1 );
function jkws_mainwp_added_new_site_save_webhost( $site_id ) {
	$webhost = isset( $_POST['mainwp_managesites_edit_webhost'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_managesites_edit_webhost'] ) ) : '';
	apply_filters( 'mainwp_updatewebsiteoptions', false, $site_id, 'webhost', $webhost );
}

And everything worked as expected, while adding a new site, I was able to save the Host info.

On a side note, have you thought about making the field as field-type dropdown where you can list 10-20 most popular hosts plus an “Other” option that shows option for user to add custom host?

1 Like

My developer skills are quite limited and I am glad you can’t explain it yet either, so that implies it’s not just a stupid mistake. :wink:

A dropdown list would be a nice addition. If you want you can add it to my Issues list on Github an idea/enhancement. :slight_smile: