Skip to:
Content
Pages
Categories
Search
Top
Bottom

I want to add “topic’s thumbnail field” in new topic post


  • jappan
    Participant

    @jappan

    I want to add topic’s thumbnail filed to specify the image file of the thumbnail of the topic in the topic creation item.
    But i can’t.
    Even if I specify an image file, only the name of the file is output, and maybe the image itself cannot be saved.
    I added this code in “functions.php”.
    If I make a mistake, Please let me know.

    add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
    function bbp_extra_fields() {
       $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field1', true);
       echo '<label for="bbp_extra_field1">URL</label><br>';
       echo "<input type='url' name='bbp_extra_field1' value='".$value."'>";
    
       $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field2', true);
       echo '<label for="bbp_extra_field1">pic</label><br>';
       echo "<input type='file' name='bbp_extra_field2' value='".$value."'>";
    }
    
    add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 );
    add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 );
    
    function bbp_save_extra_fields($topic_id=0) {
      if (isset($_POST) && $_POST['bbp_extra_field1']!='')
        update_post_meta( $topic_id, 'bbp_extra_field1', $_POST['bbp_extra_field1'] );
      if (isset($_POST) && $_POST['bbp_extra_field1']!='')
        update_post_meta( $topic_id, 'bbp_extra_field1', $_POST['bbp_extra_field2'] );
    }
    
    add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
      $topic_id = bbp_get_topic_id();
      $value1 = get_post_meta( $topic_id, 'bbp_extra_field1', true);
      $value2 = get_post_meta( $topic_id, 'bbp_extra_field2', true);
      echo "Field 1: ".$value1."<br>";
      echo "Field 2: ".$value2."<br>";
    }

    This is the error I am facing!

Viewing 13 replies - 1 through 13 (of 13 total)

  • Robin W
    Moderator

    @robin-w

    you need to wrap the url into an image tag as in

    https://www.w3schools.com/tags/tag_img.asp

    so (untested so may need fixing) and image sizing but try

    add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
      $topic_id = bbp_get_topic_id();
      $value1 = get_post_meta( $topic_id, 'bbp_extra_field1', true);
      $value2 = get_post_meta( $topic_id, 'bbp_extra_field2', true);
      $img1 = '<img src="'.$value1.'">' ;
      echo "Field 1: ".$img1."<br>";
      echo "Field 2: ".$value2."<br>";
    }

    jappan
    Participant

    @jappan

    Thank you as always. Mr Robin.
    I tried the code but it didn’t work.
    so i would like to know again.

    I have set up a third field for clarity.
    ↓this is my code↓

    add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
    function bbp_extra_fields() {
       $value = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field1', true);
       echo '<label for="bbp_extra_field1">URL</label><br>';
       echo "<input type='url' name='bbp_extra_field1' value='".$value."'>";
    
       $value11 = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field2', true);
       echo '<label for="bbp_extra_field2">pic</label><br>';
       echo "<input type='file' name='bbp_extra_field2'γ€€ value='".$value11."'>";
    }
    
    add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 );
    add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 );
    
    function bbp_save_extra_fields($topic_id=0) {
      if (isset($_POST) && $_POST['bbp_extra_field1']!='')
        update_post_meta( $topic_id, 'bbp_extra_field1', $_POST['bbp_extra_field1'] );
      if (isset($_POST) && $_POST['bbp_extra_field2']!='')
        update_post_meta( $topic_id, 'bbp_extra_field2', $_POST['bbp_extra_field2'] );
    }
    
    add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
      $topic_id = bbp_get_topic_id();
      $value1 = get_post_meta( $topic_id, 'bbp_extra_field1', true);
      $value2 = get_post_meta( $topic_id, 'bbp_extra_field2', true);
      $img1 = '<img src="'.$value2.'">' ;
      echo "Field 1: ".$value1."<br>";
       echo "Field 2: ".$value2."<br>";
    	echo "Field 3: ".$img1."<br>";	
    }



    Robin W
    Moderator

    @robin-w

    ok, I’ll give it one more try, and then you are on your own!!

    your code would produce

    <img src="84396884_480x480.jpg">

    which is not a valid url, so produces nothing.

    you need a link which is a valid url to an image, so one where you see an image if you put the url into your browser


    jappan
    Participant

    @jappan

    I understand that there was a problem with the file I uploaded…
    So, I have to convert the arbitrarily uploaded file to a URL.
    Or I have to upload the image to the server once,right?


    Robin W
    Moderator

    @robin-w

    yes, unless you can can tell the server precisely where to find the image, it cannot display it πŸ™‚


    jappan
    Participant

    @jappan

    Could you please give me the proper code? I really appreciate it.


    Robin W
    Moderator

    @robin-w

    I did give you the proper code – you need to link to the full url of an image ie a file that is an image file


    Robin W
    Moderator

    @robin-w


    Robin W
    Moderator

    @robin-w

    as

    <img src="http://www.kimberleygundogs.com/wp-content/uploads/2019/12/dog-jumping.jpg" alt="" />

    gives you


    jappan
    Participant

    @jappan

    I’m sorry to ask you many questions.

    I asked a question before, but I haven’t solved it yet.
    so I really need a code to solve it.

    I want to add “http://www.example.com/wp-content/uploads/β—‹β—‹&#8221; to the image.

    This is the code I made from Mr.Robin, but i haven’t been able to add the url to the image and display it yet.

    I’ve been studying for 2 days, and expecting that using <?php $upload_dir = wp_upload_dir(); ?>will come closer to solution, but…


    add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields');
    function bbp_extra_fields() {
    $value11 = get_post_meta( bbp_get_topic_id(), 'bbp_extra_field2', true);
       echo '<label for="bbp_extra_field2">pic</label><br>';
       echo "<input type='file' name='bbp_extra_field2'  accept='.jpg,.jpeg,.png,.gif'γ€€ value='".$value11."'>";
    }
    add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 );
    add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 );
    function bbp_save_extra_fields($topic_id=0) {
    	if (isset($_POST) && $_POST['bbp_extra_field2']!='')
        update_post_meta( $topic_id, 'bbp_extra_field2',$_POST['bbp_extra_field2'] );
    }
    add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields');
    function bbp_show_extra_fields() {
      $topic_id = bbp_get_topic_id();
      $value2 = get_post_meta( $topic_id, 'bbp_extra_field2', true);
      $img = '<img src=".$value2.">' ;
     echo "pic: ".$value2."<br>";
     echo "picture:".$img."<br>";	
    }

    Robin W
    Moderator

    @robin-w

    ok, so in the example above, what does $value2 equal

    $value2 = get_post_meta( $topic_id, 'bbp_extra_field2', true);

    and what is the full url that you want?


    jappan
    Participant

    @jappan

    I want to display the image file arbitrarily uploaded by the the poster on the topic screen in the bulletin board part of this site.

    https://tokyolyric.com/forums/


    jappan
    Participant

    @jappan

    so I expect that it will be necessary to add “http://tokyolyric.com/wp-content/uploads/〇〇&#8221; url.

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.
Skip to toolbar