Got it.
I was already using the Advanced Custom Fields plugin so I just created a custom field called “old_url” with that.
Then I created a function:
function sharingurl () {
if(get_field('old_url') == "")
{
echo untrailingslashit(get_permalink());
} else {
echo get_field('old_url');
}
}
Then I just included <?php sharingurl(); ?>
in the data-href and data-counturl for Facebook and Twitter codes respectively.
Note: Facebook does not like trailing slashes, that’s why I had to remove it from the permalink.