Codes vary by location. However, if you use a junction box, the wires are properly secured, and said box isn't concealed, you should be OK. If that box is accessible through the attic or you're running the wiring off an existing outlet box, you should be good. Just be sure things are properly secured. Obviously, it'd be closer to code than the existing work. I just noticed the existing work, on that back wall, is definitely not up to code.