Please or Register to create posts and topics.

Saving custom field value for new topic

Hi there!

I’m trying to save a custom field value (color) for each new topic.

So far I set up the WP iris colorpicker below every topic form (I used the asgarosforum_editor_custom_content_bottom hook for that). But now I need some help saving this field and getting the data. While searching I stumbled upon the asgarosforum_after_add_topic_submit hook, but I got the feeling that this hook is too late.

Is there a hook I’m missing? Or is there another approach I could try? 🙂

Thanks!

Hello @raki

The asgarosforum_after_add_topic_submit hook should be the correct one. Here is an example:

function save_color($post_id, $topic_id, $subject, $content, $link, $author_id) {
    if (!empty($_POST['forum_color'])) {
        // Save it somewhere.
    }
}
add_action('asgarosforum_after_add_topic_submit', 'save_color', 10, 6);

The only thing you have to think about is where you want to save it. For example you can but it into an field in the WordPress options-table – accessible by an key based on the topic-id: asgarosforum_topic_1234_color.

Raki has reacted to this post.
Raki
If you want to support the development of Asgaros Forum, you can leave a good review or donate. Thank you very much!

Hi @asgaros

Thanks for your fast reply and your help! Gah, sometimes it’s just too easy 😉

One last question though: Is there a filter for editing the row-view? I’d like to add a custom attribute to the div.topic-element in the topic overview.

Hello @raki

Yes, you can add custom columns to a topic-row with the asgarosforum_custom_topic_column hook. It uses one parameter: The ID of the topic.

If you want to support the development of Asgaros Forum, you can leave a good review or donate. Thank you very much!

Hi @asgaros

I’ve found that hook (and I’ll use that if there’s no other option) – I was just wondering if there is a hook for modifying the div.topic itself (instead of adding another column into  it). 🙂

Quote from Asgaros on December 17, 2018, 12:32 pm

Hello @raki

The asgarosforum_after_add_topic_submit hook should be the correct one. Here is an example:

function save_color($post_id, $topic_id, $subject, $content, $link, $author_id) {
    if (!empty($_POST['forum_color'])) {
        // Save it somewhere.
    }
}
add_action('asgarosforum_after_add_topic_submit', 'save_color', 10, 6);
  1. function save_color($post_id, $topic_id, $subject, $content, $link, $author_id) {
  2. if (!empty($_POST[‘forum_color’])) {
  3. // Save it somewhere.
  4. }
  5. }
  6. add_action(‘asgarosforum_after_add_topic_submit’, ‘save_color’, 10, 6);
function save_color($post_id, $topic_id, $subject, $content, $link, $author_id) {
    if (!empty($_POST['forum_color'])) {
        // Save it somewhere.
    }
}
add_action('asgarosforum_after_add_topic_submit', 'save_color', 10, 6);

The only thing you have to think about is where you want to save it. For example you can but it into an field in the WordPress options-table – accessible by an key based on the topic-id: asgarosforum_topic_1234_color.

Hi @asgaros

I’ve saved the new data with add_option(), but how do I associate it with the topic-id? Thanks so much!

Hello @jackfukushima

You can for example save the topic-ID with the data itself inside of an array:

$var = array();
$var['topic_id'] = 1337;
$var['data'] = 'Your data ...';

 

If you want to support the development of Asgaros Forum, you can leave a good review or donate. Thank you very much!

Hi @asgaros

Thanks for the reply! I was actually trying to save the option of a custom input field for that specific topic. Is there a way to do this?

Thanks so much!

@jackfukushima Of course, but it depends on your PHP-implementation. If you use custom database-tables for it you have to use a SQL-query. Otherwise you can save it inside of the options using an identifier based on the topic-ID and the custom input-name.

Here is an additional example:

function save_color($post_id, $topic_id, $subject, $content, $link, $author_id) {
    if (!empty($_POST['topic_color'])) {
        add_option('topic_color'.$topic_id, $_POST['topic_color']);
    }
}
add_action('asgarosforum_after_add_topic_submit', 'save_color', 10, 6);

 

If you want to support the development of Asgaros Forum, you can leave a good review or donate. Thank you very much!

Hi @asgaros

This worked beautifully. Thank you so much!