Forum Replies Created

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter AndradeL

    (@womday)

    Thank you for your reply. I immediately went trying, hence my lack of immediate response.

    Your post was spot on and I could commit and stick to that, rather then switching between certain hooks, not knowing wether they were to be used or not (just as wp_insert_attachment).

    I have succesfully created a post, by uploading text from my form and by attaching an image by doing a upload (input type=’file’).
    Then I successfully got the image to be added to MEDIA and then to attach it’s ID to the post.

    The following code is just plain, without the security features.

    
    session_start();
    
    Based on my current directory path:
    // 
    require ('../includes/functions.php');
    require_once('../../../../wp-load.php');
    
    // required for uploading
    require_once('../../../../wp-admin/includes/file.php'  );
    require_once('../../../../wp-admin/includes/image.php' );
    require_once('../../../../wp-admin/includes/media.php' );
    
    // END requirements uploaden
    
    //The following $_POSTS are coming from my HTML form
    
    $titel             = $_POST['titel'];
    $selectedOnderwerp = $_POST['selectedOnderwerp'];//subject
    $berichtTextarea   = $_POST['berichtTextarea']; //textarea
    $uploadedFileValue = $_FILES['uploadedFileValue']; //<input type = 'file' name='uploadedFileValue'>
    $filename          = $_FILES['uploadedFileValue']["name"]; //name of the file
    $fileType          = $_FILES['uploadedFileValue']["type"];
    

    Not persé needed

    
    global $user_ID;
    global $post;
    global $post_id;
    
    
    // The data for the post
       $new_post = array(
      'post_title'    => $titel,
      'post_content'  => $berichtTextarea,
      'post_status'   => 'publish',
      'post_date'     => date('Y-m-d H:i:s'),
      'post_author'   => $user_ID,
      'post_type'     => 'post',
      'post_category' => array($selectedOnderwerp),
      'post_author'   => get_current_user_id(),
    );
    

    Now create the post

    
    $post_id = wp_insert_post($new_post);
    

    Get the current POST ID

        
    $currentPostID = $post_id;
    
    echo $currentPostID;
    

    And attach the attachmentFile to the post_id

    
    // once more, 'uploadedFileValue' is the name of my input file in html form
    $attachment_id = media_handle_upload( 'uploadedFileValue', $post_id );
    
    // from here I redirect  the user
    header( 'location: ../pages/page-overzicht.php');
    exit;
    

    Then to display that post:

    In have created an attachPost.php with the following content

    
    <?php
    session_start();
      require_once( '../../../../wp-load.php');
    
      // required for gettinng attachments
    
      require_once(  '../../../../wp-includes/post.php'   );
      require_once(  '../../../../wp-includes/plugin.php' );
      require_once(  '../../../../wp-includes/media.php'  );
    

    Then in page-display.php

    
    <?php
      include_once '../pages/attachPost.php';
      $xy = query_posts( 'post_type=post&posts_per_page=10');
    ?>
    

    You can do the styling by yourself, just plain php:

    
    <div class="itemClusterContainer">
      <?php
    
          foreach($xy as $va => $key) :?>
    
            <div class="itemCluster">
                <div>
                    <img src="../img/body/MediaVormgeven-en-Ict.svg" alt="gears">
                </div>
    
                <span class="itemClusterTextClass"><?php echo $key->post_title;?></span>
                <br>
                    <a class="buttonMain" href="../pages/page-article.php?id=<?php echo $key->ID;?>">Bekijk</a>
                <br>
            </div>
    
      <?php endforeach ?>
     </div>
    

    The above was a sumary page, when you click on the actual button you get the data of the post as following on

    page-article.php

    
    <?php
    session_start();
    include_once './attachPost.php';
    
    
    $postIdNumber = $_GET['id'] // from the button you have clicked to come here;
    
    //Get the media
    $mediaArticle = get_attached_media( '', $postIdNumber);
    
    $postIdNumber = get_post( $postIdNumber );
    $title = $postIdNumber->post_title;
    ?>
    

    Show the title

    
    
    <div id="wpBericht" class="itemCluster">
    
                  <?php
                   $postContent = get_post( $postIdNumber );
                   if($postContent)
                     {
                       $title = $postContent->post_content;
                       echo $title;
                       echo '<br><br><br>';
                     }
                    else{
                      echo 'Er is geen bericht aan de post toegevoegd <br><br>';
                    }
    
                  ?>
           </div>
    

    Show the content of the upload, somewhat modified for here

    
    <?php
    
                foreach ($mediaArticle as $articleInfo => $key)
                {
                  if ($key->post_mime_type === 'image/png' || $key->post_mime_type === 'image/jpg' || $key->post_mime_type === 'image/jpeg'){?>
                          <img class="imageSecTwo" src="<?php echo $key->guid; ?>" />
                          <p>
                            <a href="<?php echo $key->guid;?>" download>
                            Download: <u><?php echo $key->post_title;?></u>
                            </a>
                          </p>
                        <?php
                      }
                    else{
                      echo 'This post has no attachment';
                    }
                }
        ?>
    

    I am very happy with your spot on help.
    If this forum allowed me to +rep you I would have done so.

    God bless you @bcworkz

    • This reply was modified 2 years, 5 months ago by AndradeL.
    • This reply was modified 2 years, 5 months ago by AndradeL. Reason: Better markup
    • This reply was modified 2 years, 5 months ago by AndradeL. Reason: + posting the content
    • This reply was modified 2 years, 5 months ago by AndradeL.
    • This reply was modified 2 years, 5 months ago by AndradeL. Reason: More code for completion
    • This reply was modified 2 years, 5 months ago by AndradeL. Reason: Forgot to close the `tag`
    • This reply was modified 2 years, 5 months ago by AndradeL. Reason: Final edit. Deleted code that is not needed for you
    • This reply was modified 2 years, 5 months ago by AndradeL.
    Thread Starter AndradeL

    (@womday)

    So I have started a project which deadline is ending soon.

    The custom theme and everyhting has been setup.
    The only thing that is required, is to have a overview page whichs shows all of the posts made with the attachments.

    Is it correct that I need to seek the attachment by searching for the ‘post ID’, and then retreive it by using SQL?

    Where I then use HTML for the markup?

    QUOTE “If the image is to be in post content, you could append or prepend a HTML img tag (or image block) to post content”

    Is there a page which explains how to summon that content?

    Also, I’d like this feature to be implemented because WordPress already adds an image (or file attachment) to a post in ‘wp-admin/posts’. It’s already happening when a post is made so a hook for it shouldn’t be ‘that’ much of a hassle? Where would I be able to file a feature request sir?

    Thread Starter AndradeL

    (@womday)

    Hi, thank you for the reply.

    Quote ‘Why wouldn’t you use media_handle_upload()?’
    Answer: because wp_insert_attachment actually conntected to the SQL, so I went on with that hook.

    I have managed to get the file to upload and be visible when logged in to ‘wp-admin and click within the menu on Media’ by using your advice, but I’m not there yet.

    
    <?php
    session_start();
    // require '../includes/functions.php';
    require_once '../../../../wp-load.php';
    require_once '../../../../wp-admin/includes/file.php';
    
    // Verplichte requires voor de uploaden
    require_once(  '../../../../wp-admin/includes/image.php' );
    require_once(  '../../../../wp-admin/includes/media.php' );
    
    // END Verplichte requires voor de uploaden
    
    $titel             = $_POST['titel'];
    $selectedOnderwerp = $_POST['selectedOnderwerp'];
    $berichtTextarea   = $_POST['berichtTextarea'];
    $uploadedFileValue = $_FILES['uploadedFileValue'];
    $filename          = $_FILES['uploadedFileValue']["name"];
    $fileType          = $_FILES['uploadedFileValue']["type"];
    
    global $user_ID;
    global $post;
    global $post_id;
    
    $new_post = array(
      'post_title' => $titel,
      'post_content' => $berichtTextarea,
      'post_status' => 'publish',
      'post_date' => date('Y-m-d H:i:s'),
      'post_author' => $user_ID,
      'post_type' => 'post',
      'post_category' => array($selectedOnderwerp),
      'post_author' => get_current_user_id(),
    );
    
    //workin post methode
    $post_id = wp_insert_post($new_post);
    
    //The following succesfully ads the image (or PDF) to the SQL, but also to the
    // backend in wp-admin/ media -> (image / attachment visible here)
    // but whatever I do the image / attachment can't be found in posts -> the actual post
    
    $attachment_id = media_handle_upload( 'uploadedFileValue', $post_id );
    
    $wp_upload_dir = wp_upload_dir();
    
    $data = array(
        'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
        'post_mime_type' => $fileType,
        'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
        'post_content'   => '',
        'post_status'    => 'inherit'
    );
    
    //attach meta data as instructed to the post
    wp_update_attachment_metadata( $currentPostID, $data );
    

    I have been going for over 48+ hours and would really appreciate if possible, the help, if you could help and or write the final part, so that the image gets into the WordPress post, so I can edit the styling from the WordPress editor when logged in.

    Thread Starter AndradeL

    (@womday)

    I can’t edit my message to include code tags.
    My apologies.

    Thread Starter AndradeL

    (@womday)

    Thankyou for your answer.

    Perhaps if I narrow the question, is there any way to force this way of writing into the
    3/4+1/4 colum? This is the only colum I will be using.

    url link
    Kind regards,
    Richardson

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