Search code examples
phpwordpressimagesource

How to make image name with post_id wordpress


I have a project to auto make images with each post id so i have to save each separated image associated with each post and the image name should be post id i tried these methods but not succeed

wp-content/uploads/2014/<?php the_ID(); ?>.png

===================================

$id = get_the_ID();

then

file_put_contents('wp-content/uploads/2014/text-$id.png', $image);

so i need a php method to save image with post id in ths line

file_put_contents('wp-content/uploads/2014/IMAGENAMEPOSTIDHERE.png', $image);

THANKS


Solution

  • If $image is already fine, then just simple use concatenation or use double quotes to use that ID:

    file_put_contents('wp-content/uploads/2014/text-$id.png', $image);
    // you're using single quotes, variables will not be interpolated
    

    Either:

    $id = get_the_ID();
    file_put_contents('wp-content/uploads/2014/text-'.$id.'.png', $image);
    

    or

    file_put_contents("wp-content/uploads/2014/text-$id.png", $image);
    

    Note: Assuming you have proper permissions.